Twig Tagss

19 flashcardsby Gaetan
Study these flashcardsFree · spaced repetition · iPhone, Android, Mac & web

All cards

1
apply
The apply tag allows you to apply Twig filters on a block of template data.
2
autoescape
Whether automatic escaping is enabled or not, you can mark a section of a template to be escaped or not by using the autoescape tag.

/!\ Complex tag, see doc for details.
3
block
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.

/!\ Complex tag, see doc for details.
4
deprecated
Twig generates a deprecation notice (via a call to the trigger_error() PHP function) where the deprecated tag is used in a template.

See doc for details
5
do
The do tag works exactly like the regular variable expression ({{ ... }}) just that it doesn't print anything:

{% do 1 + 2 %}
6
embed
The embed tag combines the behavior of include and extends. It allows you to include another template's contents, just like include does. But it also allows you to override any block defined inside the included template, like when extending a template.
See doc for details
7
extends

/!\ Complex tag, see doc for details.
8
flush
The flush tag tells Twig to flush the output buffer:

{% flush %}
9
for
Loop over each item in a sequence.
Inside of a for loop block you can access some special variables:

loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration
loop.last True if last iteration
loop.length The number of items in the sequence
loop.parent The parent context

/!\ Complex tag, see doc for details.
10
include
The include statement includes a template and outputs the rendered content of that file

/!\ Complex tag, see doc for details.
11
import
The import tag imports macro names in a local variable.

The tag is documented in detail in the documentation for the macro tag.
12
macro
Macros are comparable with functions in regular programming languages. They are useful to reuse template fragments to not repeat yourself.

Macros are defined in regular templates.

/!\ Complex tag, see doc for details.
13
sandbox
The sandbox tag can be used to enable the sandboxing mode for an included template, when sandboxing is not enabled globally for the Twig environment:

{% sandbox %}
{% include 'user.html' %}
{% endsandbox %}
14
set
Inside code blocks you can also assign values to variables. Assignments use the set tag and can have multiple targets.

Here is how you can assign the bar value to the foo variable:

{% set foo = 'bar' %}
15
use
Horizontal reuse is a way to achieve the same goal as multiple inheritance, but without the associated complexity:

{% extends "base.html" %}

{% use "blocks.html" %}

{% block title %}{% endblock %}
{% block content %}{% endblock %}
The use statement tells Twig to import the blocks defined in blocks.html into the current template (it's like macros, but for blocks):

{# blocks.html #}

{% block sidebar %}{% endblock %}
In this example, the use statement imports the sidebar block into the main template. The code is mostly equivalent to the following one (the imported blocks are not outputted automatically):

{% extends "base.html" %}

{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}

/!\ Complex tag, see doc for details.
16
verbatim
The verbatim tag marks sections as being raw text that should not be parsed. For example to put Twig syntax as example into a template you can use this snippet:

{% verbatim %}
<ul>
{% for item in seq %}
<li>{{ item }}</li>
{% endfor %}
<l>
{% endverbatim %}
17
with
Use the with tag to create a new inner scope. Variables set within this scope are not visible outside of the scope:

{% with %}
{% set foo = 42 %}
{{ foo }} {# foo is 42 here #}
{% endwith %}
foo is not visible here any longer
18
if
The if statement in Twig is comparable with the if statements of PHP.

In the simplest form you can use it to test if an expression evaluates to true
19
from
The from tag imports macro names into the current namespace. The tag is documented in detail in the documentation for the macro tag.
Study all 19 cards for free