Search code examples
twig

whats the difference between {% vs {%- in twig?


as can be seen here https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig some of the if conditions are written as:

{% if condition %}

but others are written as:

{%- if condition -%}

my code works perfectly fine with both versions. unfortunately its impossible to google the answer as google doesn't like no alphanumeric chars in query very much, also i did not find any reference in twig documentation.

my question is, whats the difference between those two?

whats the meaning of the dash in {%- ?


Solution

  • It is related to twig's whitespace control. By using the whitespace control modifier on your tags, you can trim leading and/or trailing whitespace. Here are the docs.