set
setΒΆInside code blocks you can also assign values to variables. Assignments use
the set tag and can have multiple targets:
{% set foo = 'foo' %}
{% set foo = [1, 2] %}
{% set foo = {'foo': 'bar'} %}
{% set foo = 'foo' ~ 'bar' %}
{% set foo, bar = 'foo', 'bar' %}
The set tag can also be used to 'capture' chunks of text:
{% set foo %}
<div id="pagination">
...
</div>
{% endset %}
Caution
If you enable automatic output escaping, Twig will only consider the content to be safe when capturing chunks of text.