Marco
Marco

Reputation: 3651

Automatic spaceless for twig templates

I've a project using twig with a lot's of html templates. Unfortunatelly there is no "master"-templates which loads these templates. So each twig templates is standalone.

Is it possible to configure twig in that way, that the %spaceless% function will be applied to every output? https://twig.symfony.com/doc/2.x/filters/spaceless.html

{% apply spaceless %}
    <div>
        <strong>foo</strong>
    </div>
{% endapply %}

Sure - it's possible to set these into every single twig template, but that's a quite dumb idea, because for every new template you have to "remember".

My idea is to implement this directly (maybe an extension) into twig, but I just didn't find the right approach for doing this? Or is there a twig option to enable this feature by default?

Upvotes: 1

Views: 2395

Answers (1)

Schnodderbalken
Schnodderbalken

Reputation: 3487

There are actually numerous options you can approach this. I think most of them are already covered here:

symfony every block with spaceless

You would always be in the situation to cover your code within these brackets.

But what does actually stop you from extending every template from a base template and surrounding the content block of that by {% spaceless %} tags? That would be clean in the way that whenever you decide to create templates without spaceless property, you can just use the usual block.

You could also do a "replace all" across all your templates and replace "block" by our own new tag.

If you really do not want a new tag/node/something, you could also write a new token parser that looks for usual blocks and applies the spaceless function there if you really don't want the necessity to "remember" using {% spaceless %}.

Upvotes: 3

Related Questions