Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does twig support executing multiple statements inside a single {% %} block?

Tags:

php

twig

Can I use construct like this in twig -

{% 
set a = 'first' 
set b = 'second' 
%}

instead of this -

{% set a = 'first' %}
{% set b = 'second' %}
like image 289
nayak Avatar asked Jun 29 '15 08:06

nayak


Video Answer


1 Answers

You cannot execute several statements inside a single {% ... %} block, but the solution provided by CodeBrauer will do the trick. Anyway, keep in mind that the number of expressions on the left and on the right of the = sign must match.

This means that if the two (or three, etc.) variables share the same value, you must repeat that value. Example:

{# this will work as expected #}
{% set a, b, c = 'value', 'value', 'value' %}

{# this won't work #}
{% set a, b, c = 'value' %}
like image 89
Javier Eguiluz Avatar answered Oct 03 '22 04:10

Javier Eguiluz