0

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 %}
     <div class="stuff">
         <ul class="somestuff">
             {% for background in block.slide %}
                 <li style="background-image:url({{ background.url() }})"></li>
             {% endfor %}
         </ul>
     </div>

     <section id="text-slider">
         <div class="container">
             {% block child %}{% endblock %}
        </div>
     </section>
 {% 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.

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!

  • 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 Jan 4 '17 at 21:26
  • 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 Jan 4 '17 at 21:37
0

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.

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.