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

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

<p><strong>page.twig</strong></p>

<pre><code>{% include "_snippets/thingy" with { active: lightswitchTest } %}
</code></pre>

<p><strong>thingy.twig</strong></p>

<pre><code>&lt;div class="panel{% if not active %} notActive{% endif %}"&gt;
    {# bla bla #}
&lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2016-10-24T15:31:57.793Z
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
CC BY-SA 3.0
2016-10-24T11:21:56.367Z
2016-10-24T11:21:56.367Z
Mike Pepper
<p>I have a snippet within a template which I want to pass a variable into:</p>

<p><strong>page.twig</strong></p>

<pre><code>{% set lightswitchTest = entry.aktivesProjekt('1').find() %}

{% include '_snippets/thingy' %}
</code></pre>

<p><strong>thingy.twig</strong></p>

<pre><code>&lt;div class="panel{% if lightswitchTest == null %} notActive{% endif %}"&gt;
   {# bla bla #}
&lt;/div&gt;
</code></pre>

<p>I tried to add pass in the variable with the <a href="http://twig.sensiolabs.org/doc/tags/include.html" rel="nofollow">with keyword</a> but I guess my syntax is wrong?</p>

<pre><code>{% include '_snippets/thingy' with lightswitchTest %}
</code></pre>

<p>If i use the varibale now in the snippet there is just the <em><code>Array to string conversion</code></em> error.</p>

<p>What am I missing? Is there a better way?</p>

<variable><include>
Pass variable into include
# Pass variable into include

(Asked by: username_0 on 2016-10-24)

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

   {# bla bla #}


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?

**Tags:** variable, include

---

## 1 answers

### Answer by username_1 on 2016-10-24. Score: 14 | Accepted answer

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

    {# bla bla #}

> **Comments:**
> **username_2**: 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 %}`  

---

