We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Minified source : <pre> tags trouble

Hi,

I have some troubles to render correctly <pre> tags.

Can you tell me how to get a correct render or disable minification please ?

Thanks



32.3k
Accepted
answer

hi @grizour are you minify html? maybe this regexp helps you


 preg_replace(['#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+)(?:<(?>textarea|pre|script)\b|\z))#',
                '#>\s+<#'],
                [' ', '><'],
                $content);

This prevent minification in textarea, pre and script tags Good luck



520

Thank you so much ! This regexp works like a charm :)

I was looking at the wrong way, I thought it was the template engine who minified the results. Actualy, it is the last line of the entry file : /public/index.php

    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);

    // echo str_replace(["\n","\r","\t"], '', $application->handle()->getContent());
    echo preg_replace(['#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+)(?:<(?>textarea|pre|script)\b|\z))#',
                        '#>\s+<#'],
                        [' ', '><'],
                        $application->handle()->getContent()
    );

You can also not minimize the html result :

    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);

    echo $application->handle()->getContent();

Thanks again ! Grizour.

Yes it is a pain, I did make a bootstrap and put it there


class Bootstrap extends \Phalcon\Mvc\Application 
{
...
    public function init()
    {
        $debug = new \Phalcon\Debug();
        $debug->listen(true, true);

        \Phalcon\Mvc\Model::setup([
            'castOnHydrate'      => true,
            'notNullValidations' => false, 
        ]);

        $this->_registerServices();
        $this->registerModules($this->modules);

        $content = $this->handle()->getContent();

        if (\Core\Model\Config::get(\Core\Model\Config\Performance::MINIFY_HTML)) {
            $content = preg_replace([
                '#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+)(?:<(?>textarea|pre|script)\b|\z))#',
                '#>\s+<#'],
                [' ', '><'],
                $content);
        }

        return $content;
    }
...
}