Providing clean and neat HTML with TWIG templates

I played with Twig(version 1.9.1) as a template PHP, and I'm not completely satisfied with the displayed HTMLwhen extra unwanted tears and spaces are added that make the result pretty messy. To make my templates more flexible, I use ones macrosthat can call each other in a nested way, which in itself works just fine, but seems to make things worse. For example, an item is <a>displayed as follows:

             <a href="http://google.com" alt="some alternative text">                    some text with <strong>some html</strong>



</a>

I know that the reason is because my templates are Twigformatted as if I were deleting empty lines and indenting them, the rendering HTMLlooks a little more neat, but not completely: the output below shows <a>two more line breaks between the beginning and the end of the element, despite that the corresponding template does not contain empty lines or spaces! :

<a href="http://google.com" alt="some alternative text">some text with <strong>some html</strong>


</a>

Even if that helps, removing formatting from my templates Twig(i.e. blank lines and indents) is not really an option, since my template is very difficult to read and maintain.

Besides removing formatting from templates. what are some ways to make cleaner / cleaner HTML with Twig?

+5
source share
3 answers

, . : https://gist.github.com/urraka/ccd1812570ca4b278b9f

gist .

, :

{# Given variable = "line1\nline2\nline3" #}

<body>
    {% if true %}
        <stuff>{% if true %}only blocks that take the whole line have effect{% endif %}</stuff>
        {{ variable }}
    {% endif %}
</body>

:

<body>
    <stuff>only blocks that take the whole line have effect</stuff>
    line1
    line2
    line3
</body>
+1

Although this package is no longer supported, it might start recording a neat service using tidy_parse_string (). KnpMarkupValidatorBundle

0
source

All Articles