<p>The correct way of doing this for anybody interested is as I had guessed, using embed rather than include but also to setup a recursive macro like so in Template C:</p>

<pre><code>{% macro recursiveBlock(block) %}
  {% import _self as self %}
  {% embed 'Template D' %}
      {% if block.children is not empty %}
          {% block child %}
              {% for block in block.children %}
                  {{ self.recursiveBlock(block) }}
              {% endfor %}
          {% endblock %}
      {% endif %}
  {% endembed %}
{% endmacro %}

{% from _self import recursiveBlock %}

{% for block in entry.body.level(1) %}
    {{ recursiveBlock(block) }}
{% endfor %}
</code></pre>

<p>Template D remains unchanged.</p>

<p>While Sam's answer was correct for a much simpler need, this is the way it had to be done (as far as I understand) for something more complex.</p>

CC BY-SA 3.0
2017-01-05T01:32:48.783Z
2017-01-05T01:32:48.783Z
Christopher Healey
<p>You could use <code>include...with</code>, as specified in the Twig Docs <a href="http://twig.sensiolabs.org/doc/tags/include.html" rel="nofollow noreferrer">here</a></p>

<p>Example for your case:</p>

<pre><code>{# in Template C #}
{% include 'template-d.twig' with {'foo':'Hello World'} %}
</code></pre>

<p>This should give Template D access to <code>foo</code>. Hope that helps!</p>

CC BY-SA 3.0
2017-01-04T21:26:07.813Z
Sam, i may have over simplified my question, the content in Template C is not just a simple string but actually a big bulk of content with includes of it's own.
Christopher Healey
CC BY-SA 3.0
2017-01-04T21:37:13.520Z
Ah I see, sorry - it may be beneficial to post a bit more of the code, so users can get a general idea of what you're trying to accomplish. Sorry I couldn't be of more help!
Sam LeBlanc
CC BY-SA 3.0
2017-01-04T21:24:59.643Z
2017-01-04T21:24:59.643Z
Sam LeBlanc
<p>I have a layout template, we'll call that <strong>Template A</strong> that is extended by another template, we'll call that <strong>Template B</strong> that includes another template, we'll call that <strong>Template C</strong> that includes another template lets call that <strong>Template D</strong></p>

<p>So the visually it goes like so:</p>

<p><strong>-Template A</strong></p>

<p><strong>--Template B</strong></p>

<p><strong>---Template C</strong></p>

<p><strong>----Template D</strong></p>

<p>In Template C I define some content such as "<strong>Hello World</strong>" as well as a bunch of other content.</p>

<p>I do not want Template C to output the "<strong>Hello World</strong>" content but instead want <strong>Template D</strong> to decide where it is output as defined in <strong>Template C</strong></p>

<p>I'm thinking that somewhere i might should use <em>embed</em> rather than <strong>include</strong> but i'm a bit hazy on exactly how to achieve this. Any help would be greatly appreciated.</p>

<p><strong>NOTE:</strong> The content defined in <strong>Template C</strong> is defined <em>after</em> <strong>Template C</strong> includes <strong>Template D</strong> and also the content that is defined is not just a simple string as indicated above <em>"hello world"</em> but actually much more complex data.</p>

<p><strong>UPDATE:</strong> Here is an example of the templates to help better understand the question.</p>

<p><strong>Template C:</strong></p>

<pre><code>{% for block in entry.body.level(1) %}
  {% include 'Template D' %}
  {% if block.children is not empty %}
    {% block child %}
        {% for block in block.children %}
            {% include 'Template E' %}
        {% endfor %}
    {% endblock %}
  {% endif %}
{% endfor %}
</code></pre>

<p>Where Template E is the <em>"Hello World"</em> content mentioned above.</p>

<p><strong>Template D:</strong></p>

<pre><code> {% if block.slide|length %}
     &lt;div class="stuff"&gt;
         &lt;ul class="somestuff"&gt;
             {% for background in block.slide %}
                 &lt;li style="background-image:url({{ background.url() }})"&gt;&lt;/li&gt;
             {% endfor %}
         &lt;/ul&gt;
     &lt;/div&gt;

     &lt;section id="text-slider"&gt;
         &lt;div class="container"&gt;
             {% block child %}{% endblock %}
        &lt;/div&gt;
     &lt;/section&gt;
 {% endif %}
</code></pre>

<p>And more code also after that endif. Where Block Child (in Template C) is the content to be defined and Block Child (in Template D) is where i want it output.</p>

<templating><include><extends><embed>
Set Parent Template Defined Content to Output in Child Template
# Set Parent Template Defined Content to Output in Child Template

(Asked by: username_0 on 2017-01-04)

I have a layout template, we'll call that Template A that is extended by another template, we'll call that Template B that includes another template, we'll call that Template C that includes another template lets call that Template D
So the visually it goes like so:
-Template A
--Template B
---Template C
----Template D
In Template C I define some content such as "Hello World" as well as a bunch of other content.
I do not want Template C to output the "Hello World" content but instead want Template D to decide where it is output as defined in Template C
I'm thinking that somewhere i might should use embed rather than include but i'm a bit hazy on exactly how to achieve this. Any help would be greatly appreciated.
NOTE: The content defined in Template C is defined after Template C includes Template D and also the content that is defined is not just a simple string as indicated above "hello world" but actually much more complex data.
UPDATE: Here is an example of the templates to help better understand the question.
Template C:
{% for block in entry.body.level(1) %}
  {% include 'Template D' %}
  {% if block.children is not empty %}
    {% block child %}
        {% for block in block.children %}
            {% include 'Template E' %}
        {% endfor %}
    {% endblock %}
  {% endif %}
{% endfor %}

Where Template E is the "Hello World" content mentioned above.
Template D:
 {% if block.slide|length %}
     
         
             {% for background in block.slide %}
                 
             {% endfor %}
         
     

     
         
             {% block child %}{% endblock %}
        
     
 {% endif %}

And more code also after that endif. Where Block Child (in Template C) is the content to be defined and Block Child (in Template D) is where i want it output.

**Tags:** templating, include, extends, embed


---

## 2 answers

### Answer by username_0 on 2017-01-05. Score: 0 | Accepted answer

The correct way of doing this for anybody interested is as I had guessed, using embed rather than include but also to setup a recursive macro like so in Template C:
{% macro recursiveBlock(block) %}
  {% import _self as self %}
  {% embed 'Template D' %}
      {% if block.children is not empty %}
          {% block child %}
              {% for block in block.children %}
                  {{ self.recursiveBlock(block) }}
              {% endfor %}
          {% endblock %}
      {% endif %}
  {% endembed %}
{% endmacro %}

{% from _self import recursiveBlock %}

{% for block in entry.body.level(1) %}
    {{ recursiveBlock(block) }}
{% endfor %}

Template D remains unchanged.
While Sam's answer was correct for a much simpler need, this is the way it had to be done (as far as I understand) for something more complex.


---

### Answer by username_1 on 2017-01-04. Score: 1

You could use include...with, as specified in the Twig Docs here
Example for your case:
{# in Template C #}
{% include 'template-d.twig' with {'foo':'Hello World'} %}

This should give Template D access to foo. Hope that helps!

> **Comments:**
> **username_0**: Sam, i may have over simplified my question, the content in Template C is not just a simple string but actually a big bulk of content with includes of it's own.  
> **username_1**: Ah I see, sorry - it may be beneficial to post a bit more of the code, so users can get a general idea of what you're trying to accomplish. Sorry I couldn't be of more help!  

---

