Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minus In twig block definition

Tags:

twig

What's the difference between this:

{%block body %} 

and that

{%block body -%} 
like image 947
nonlux Avatar asked May 19 '13 11:05

nonlux


People also ask

What is block in twig?

Blocks are used for inheritance and act as placeholders and replacements at the same time. They are documented in detail in the documentation for the extends tag. Block names must consist of alphanumeric characters, and underscores. The first char can't be a digit and dashes are not permitted.


1 Answers

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.

EDIT

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> ... 

ouputs:

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.

like image 151
SirDerpington Avatar answered Sep 20 '22 09:09

SirDerpington