How To: Minimize HTML Output
Created 8 years ago by william

To compress the HTML Output, we need to install the following twig extension (https://github.com/nochso/html-compress-twig)

composer require nochso/html-compress-twig

Once everything is pulled in, open up twigbridge.php located in your config directory. Twigbridge accepts string and closure callbacks, so under extensions->enabled we will add the following :

function() {
    return new \nochso\HtmlCompressTwig\Extension(true);
}

It would look something like this, added to the rest of the enabled extensions:

//...
'enabled' => [
    'TwigBridge\Extension\Loader\Functions',
    function() {
        return new \nochso\HtmlCompressTwig\Extension(true);
    }
]
//...

The twig extension is set to NOT compress html as per default. Notice above we are setting it to true (do compress). If you are currently developing your app/website, i would recommend having it set to false, making it easier to debug.

Then in your main layout view, wrap all your html within these tags:

{% htmlcompress %}
Your html
{% endhtmlcompress %}

If necessary, run:

php artisan assets:clear
adnan  —  8 years ago

Not many people use that (thankfully!) cause I do click on that developer -> show code option all the time 😄 Thanks for the tip, ..

ryanthompson  —  7 years ago

@william I am going to throw this in guides as well (unless you want to but I know you're busy)