Melhor Controle de Espaços em Branco nos Templates do Twig

Andréia Bohner
Jun 29 · 4 min read

Novas Opções para Remover Espaços em Branco

<ul>
<li>
{% if some_expression %}
{{ some_variable }}
{% endif %}
</li>
</ul>
<ul>
<li>
Lorem Ipsum
</li>
</ul>
<ul>
<li>
{%- if some_expression %}
{{- some_variable -}}
{% endif -%}
</li>
</ul>
<ul>
<li>Lorem Ipsum</li>
</ul>
<ul>
<li>
{%~ if some_expression %}
{{ some_variable -}}
{% endif ~%}
</li>
</ul>
<ul>
<li>
Lorem Ipsum
</li>
</ul>

Adicionado o Filtro spaceless

{{ some_variable_with_HTML_content|spaceless }}
-{% spaceless %}
+{% apply spaceless %}
{# some HTML content here #}
-{% endspaceless %}
+{% endapply %}

Escape Refinado em Expressões Ternárias

{% set foo = '<strong>foo</strong>' %}
{% set bar = '<strong>bar</strong>' %}

{{ false ? '<strong>bar</strong>' : foo|raw }}
{# renders as '<strong>foo</strong>' #}

{{ false ? bar : foo|raw }}
{# renders as '&lt;strong&gt;foo&lt;/strong&gt;' #}

{{ (false ? bar : foo)|raw }}
{# renders as '<strong>foo</strong>' #}
{% set foo = '<strong>foo</strong>' %}
{% set bar = '<strong>bar</strong>' %}

{{ false ? '<strong>bar</strong>' : foo|raw }}
{{ false ? bar : foo|raw }}
{{ (false ? bar : foo)|raw }}
{# renders as '<strong>foo</strong>' in all cases #}

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade