Dimentica Dimentica - 4 months ago 10
Twig Question

Twig Compare a parameter passed to a template inside the template

In Twig I successfully render a template and pass to it the following array with values:

$params = array(
'value1' => 'y',
'value2' => 'x'
);


Inside the template I render, I want to have an if-statement in which to compare the value of 'value1' to a string variable.

Something like

{% set type = {{ value1 }} %}
{% if type == y %}
{* do code *}
{% elseif type == b %}
{* do code *}


How can I assign the value of {{ value1 }} to type, as this example does not work.

Joe Joe
Answer

Try: {% set type = value1 %}

But why not just:

{% if value1 == y  %}

{% elseif value2 == x %}

{% endif %}

?