Twig has the {% spaceless %}
tag, which removes all the white space between HTML tags.
However, it does this with a preg_replace when you render the template. Fine on small files, but if you have a large complex template there's a performance hit.
I feel like it should be possible to strip the whitespace out at compile time?
My compiled template is full of stuff like
echo " <h2>Heading</h2>
";
Where the template has included the following:
<section>
<h2>Heading</h2>
</section>
What I want on the final rendered HTML is
<section><h2>Heading</h2></section>
Why couldn't twig (very simply) add a trim
in when building the compiled template, to give us:
echo "<h2>Heading</h2>";
in the compiled template php code.
I get that there could still be whitespace in whatever data I populate the template with inside {{ }}
, but I can control that from my PHP easily.