Symfony2, Twig: is it better to use a block or variable?

Which solution is better and recommended for short lines? To define a block and allow the user to override its contents, for example:

<title>{% block title %}{% endblock %}</title> 

or make a block of variables, set their default values ​​and allow the user to import the reset variable that he wants, for example:

base template:

 {% block variables %} {% set meta.title = '' %} {% endblock %} <title>{{ meta.title }}</title> 

user template:

 {% block variables %} {{ parent() }} {% set meta.title = 'some title' %} {% endblock %} 
+6
source share
2 answers

I would go with the blocks. Also, remember that if you want to display the contents of a block more than once, you can use the block function:

 <title>{% block title %}{% endblock %}</title> <h1>{{ block('title') }}</h1> 
+9
source

You will need to call parent () after you set the variable for this, I think.

In any case, this is a personal preference, just use what makes your templates more understandable and understandable.

0
source

Source: https://habr.com/ru/post/915176/


All Articles