Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spaceless does not want to work correctly in Symfony2 Twig Template

Tags:

php

symfony

I expected the following:

{% spaceless %}
    blablabla
        blablabla
            blablabla
        blablabla
    blablabla
{% endspaceless %}

to return the following string:

blablabla blablabla blablabla blablabla blablabla

but in the page source, I see this:

    blablabla
        blablabla
            blablabla
        blablabla
    blablabla

How to do this without any HTML <tags>?

like image 223
user3766478 Avatar asked Oct 24 '14 14:10

user3766478


People also ask

How can we use spaceless in Twig template?

As of Twig 2.7, use the spaceless filter instead. This tag is not meant to "optimize" the size of the generated HTML content but merely to avoid extra whitespace between HTML tags to avoid browser rendering quirks under some circumstances.

How do you delete a space in twig?

Whitespace can be removed by using the modifier - in the description of Twig tags and variables.


1 Answers

As explained in the documentation, "Use the spaceless tag to remove whitespace between HTML tags, NOT whitespace within HTML tags or whitespace in plain text:"

{% spaceless %}
    <div>blablabla</div>
        <div>blablabla</div>
            <div>blablabla</div>
        <div>blablabla</div>
    <div>blablabla</div>
{% endspaceless %}

Will output:

<div>blablabla</div><div>blablabla</div><div>blablabla</div><div>blablabla</div><div>blablabla</div>
like image 144
COil Avatar answered Sep 16 '22 13:09

COil