Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2, Twig: Better use block or variable?

What solution is better and recommended for short string elements? To define block and let user override it contents like:

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

or make block of variables, set their default values and let user import this block reset variable he want like:

base template:

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

user template:

{% block variables %}
    {{ parent() }}
    {% set meta.title = 'some title' %}
{% endblock %}
like image 437
Paweł Madej Avatar asked May 08 '12 10:05

Paweł Madej


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.

Does Symfony use twig?

Templates in Symfony are created with Twig: a flexible, fast, and secure template engine.

What is Symfony twig?

Twig is the template engine used in Symfony applications. There are tens of default filters and functions defined by Twig, but Symfony also defines some filters, functions and tags to integrate the various Symfony components with Twig templates.


1 Answers

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

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

<h1>{{ block('title') }}</h1>
like image 172
Elnur Abdurrakhimov Avatar answered Oct 01 '22 06:10

Elnur Abdurrakhimov