Optimize your html with twig

Here is one cool trick I learned this week which will help you optimize your website if you are using the twig templating engine.
Timber-twig is one of my must use plugins for wordpress theme development.
During the deployment I often used to get help of plugins such as W3totalCache for html minification on the fly. This is also a great tool (You should definitely try if you never have done before).
You can still gzip the output to optimize the size of your html document.
I’m very happy to discover that feature in twig since it will generate already minified html.

One of such priceless feature is the spaceless tag

{% spaceless %}
<html>
{{#Here goes your html#}}
</html>
{% endspaceless %}

Also you can use the syntax {{- name -}} to triming
Eg

<div>
{{-name }}
</div>

gives

<div>dipesh
</div>

and

<div>
{{name- }}
</div>

gives

<div>
dipesh</div>

and as you expected


<div>
{{-name-}}
</div>

will generate


<div>Dipesh</div>

This works for print statements {% … %} and comments {# … #}

Happy code optimization!

Reference:
twig.sensiolabs.org/doc/tags/spaceless.html