<p>You can't do it exactly like that in Twig. But there're different approaches that'd do the same thing depending on exactly what you're trying to achieve. </p>

<p>For instance, you could parse the template into a variable, and then pass the variable to your include:</p>

<pre><code>{% set nav %}
    {% include 'component/nav' %}
{% endset %}

{% include 'component/footer' with { nav: nav } %}
</code></pre>

<p>Or, you could just pass in the template name as a string, and parse it inside the component/footer template. In you base template:</p>

<pre><code>{% include 'component/footer' with { navTemplateName: 'component/nav' } %}
</code></pre>

<p>Inside component/footer:</p>

<pre><code>{% include navTemplateName %}
</code></pre>

<p>What approach suits your use case depends on whether or not the scope that component/nav is parsed in, matters.</p>

CC BY-SA 3.0
2018-04-10T14:03:21.940Z
2018-04-10T14:03:21.940Z
André Elvan
<p>I'm trying to figure out how to load a template file and pass it as a variable to another template file while it is being included. In some sense I want to do something like this:</p>

<pre><code>{% include 'component/footer', with 'component/nav' %}
</code></pre>

<p>I know I can't do that directly, but in the twig <a href="https://twig.symfony.com/doc/2.x/tags/include.html" rel="nofollow noreferrer">documentation</a> there is an example showing this:</p>

<pre><code>$template = $twig-&gt;load('some_template.twig');
$twig-&gt;display('template.twig', array('template' =&gt; $template));
</code></pre>

<p>Is it possible to do the same thing with Craft's twig support? </p>

<templating><include>
Twig function to load templates into variable?
# Twig function to load templates into variable?

(Asked by: username_0 on 2018-04-10)

I'm trying to figure out how to load a template file and pass it as a variable to another template file while it is being included. In some sense I want to do something like this:
{% include 'component/footer', with 'component/nav' %}

I know I can't do that directly, but in the twig documentation there is an example showing this:
$template = $twig->load('some_template.twig');
$twig->display('template.twig', array('template' => $template));

Is it possible to do the same thing with Craft's twig support?

**Tags:** templating, include


---

## 1 answers

### Answer by username_1 on 2018-04-10. Score: 3 | Accepted answer

You can't do it exactly like that in Twig. But there're different approaches that'd do the same thing depending on exactly what you're trying to achieve. 
For instance, you could parse the template into a variable, and then pass the variable to your include:
{% set nav %}
    {% include 'component/nav' %}
{% endset %}

{% include 'component/footer' with { nav: nav } %}

Or, you could just pass in the template name as a string, and parse it inside the component/footer template. In you base template:
{% include 'component/footer' with { navTemplateName: 'component/nav' } %}

Inside component/footer:
{% include navTemplateName %}

What approach suits your use case depends on whether or not the scope that component/nav is parsed in, matters.


---

