Search code examples
phpframeworkszend-framework2twig

How to output html without whitespace getting removed in twig?


I started to use twig as template engine and I like it somehow. The only thing I don't know how to disable it, is the optimized html that it renders (newest version of twig). Twig seems to remove all unused white spaces and line breaks. In Productive Mode it is quite useful if you have a page that should have a high rank in google. But during Development it is not really usefull. So my question: How do you disable this?


Solution

  • If you use the spaceless tag twig remove whitespace between HTML tags, not whitespace within HTML tags or whitespace in plain text:

    {% spaceless %}
      <div>
        <strong>foo</strong>
      </div>
    {% endspaceless %}
    

    output will be <div><strong>foo</strong></div>

    For more information on whitespace control, read the dedicated section of the documentation and learn how you can also use the whitespace control modifier on your tags.

    Your twig version is 1.18.1 ?