Minus In twig block definition


What's the difference between this:

{%block body %}

and that

{%block body -%}

Best Solution

Just read something about it in the documentation, not sure if this will also apply on {% block ... %} tags. Twig whitespace control

{% set value = 'no spaces' %}
    {#- No leading/trailing whitespace -#}
    {%- if true -%}
        {{- value -}}
    {%- endif -%}
{# output 'no spaces' #}

There's also another example given which trims the whitespace in front of the variable but doesnt't do it at the end - so the effect is only on one side.

{% set value = 'no spaces' %}
    <li>    {{- value }}    </li>
{# outputs '<li>no spaces    </li>' #}

The above sample shows the default whitespace control modifier, and how you can use it to remove whitespace around tags. Trimming space will consume all whitespace for that side of the tag. It is possible to use whitespace trimming on one side of a tag

So I think the difference in your given exmaples is that in the first block body there will be a whitespace after the block started. In your second example body - there's none after the block started. Just read the documentation entry to see how it works.


A simple example to demonstrate the example in the docu:

{% set value = 'NO space in source code after/before "value"' %}
<li>    {{- value -}}    </li>

outputs in Firebug in the HTML markup: no whitespaces afer value

Whereas this

{% set value = 'space in source code after "value"' %}
<li>    {{- value }}    </li>


whitespace between "value" and closing </li>

Notice the space between "value" and the closing </li> in the second example. So the minus - erases/trims a whitespace either before, after or on both sides of e.g. a variable.