Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering clean & tidy HTML with TWIG templates

I've been playing with Twig (version 1.9.1) as a PHP templating engine, and I'm not totally satisfied with the rendered HTML as extra unwanted line-breaks and spaces are added which make the result quite untidy. To make my templates more flexible, I am using macros which can call each other in a nested fashion, which in itself works fine, but seems to make things worse. For instance, a <a> element is rendered as follows:

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



</a>

I know that part of the reason is the way my twig templates are formatted, as if I remove empty lines and indenting from them, the rendered HTML looks a bit more tidy, but not completely: the below output shows a rendered <a> with still 2 line breaks between the start and end of the element, despite the corresponding template not containing any empty lines or spaces!:

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


</a>

Even though it helps, removing formatting from my twig templates (i.e. empty lines and indenting) isn't really an option as it makes my template very hard to read and maintain.

Apart from removing formatting from templates. what are the ways to render a cleaner/tidier HTML with Twig?

like image 204
Max Avatar asked Aug 09 '12 07:08

Max


People also ask

What is the best way to clean render?

Professional home exterior cleaners have been using Sodium Hypochlorite aka bleach on exterior walls for many years, especially on render, there is nothing out there better or cheaper. You can mix regular bleach with water at a 1:1 ratio and spray in onto the surface using a simple pump-up sprayer.

Does rendering get dirty?

Passing traffic creates emissions which cause the rendering to fade or become dirty. Algae can start to grow where damp conditions are prevalent. Rust stains can form when rainwater mixes with metal. Over time, the building's exterior will become rundown.

How do you soft clean render?

Simply spray a product like FINAL WASH over the render you have cleaned and the area the waste wash water went over. Spray onto the render a soft wash solution between 2% and 4% to kill off any organic matter. Use a doff steam cleaner to remove graffiti or paint.

Can I pressure wash render?

Although using a pressure washer is ideal for stone and concrete surfaces, pressure washing render is not a good idea. Pressure washing rendered surfaces, such as K Rend actually strips the surface off the wall.


2 Answers

This page of the documentation should help you control spaces within tags http://twig.sensiolabs.org/doc/templates.html#whitespace-control

like image 102
gunnx Avatar answered Sep 20 '22 18:09

gunnx


I wanted correct output indentation too, so I made an extension for that. I made it available in a gist: https://gist.github.com/urraka/ccd1812570ca4b278b9f

See the usage file from the gist for a bit of an explanation.

Basically, this template:

{# 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>

would end up like this:

<body>
    <stuff>only blocks that take the whole line have effect</stuff>
    line1
    line2
    line3
</body>
like image 31
urraka Avatar answered Sep 20 '22 18:09

urraka