3

I have a snippet within a template which I want to pass a variable into:

page.twig

{% set lightswitchTest = entry.aktivesProjekt('1').find() %}

{% include '_snippets/thingy' %}

thingy.twig

<div class="panel{% if lightswitchTest == null %} notActive{% endif %}">
   {# bla bla #}
</div>

I tried to add pass in the variable with the with keyword but I guess my syntax is wrong?

{% include '_snippets/thingy' with lightswitchTest %}

If i use the varibale now in the snippet there is just the Array to string conversion error.

What am I missing? Is there a better way?

10

You can leave the with lightswitchTest part off altogether, and all the variables from the parent template will be available inside the include too.

with expects an array, where the keys correspond to the variable names and their values, so if you did want to explicitly pass that in (say as a different variable) you could use:

page.twig

{% include "_snippets/thingy" with { active: lightswitchTest } %}

thingy.twig

<div class="panel{% if not active %} notActive{% endif %}">
    {# bla bla #}
</div>
  • 3
    It's also possible to pass the only parameter, if you want to exclude other variables from being exposed to the included template: {% include "_snippets/thingy" with { active: lightswitchTest } only %} – Mats Mikkel Rummelhoff Oct 24 '16 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.