1

The structure of my Twig files looks like this:

- "skeleton_main"
  - includes "skeleteon_header"
  - render "block content"

- "skeleteon_header" should render "block breadcrumb"

- "partial"
  - extends "skeleton_main"
  - fills "block breadcrumb"

Now I can output "block breadcrumb" in "sekeleton_main" but it isn't passed to "skeleton_header". How can I access and render the block from within the included template? I tried using {% include '' with {} %} but without luck.

# skeleton_main
{% include 'header' %}
{% block content %}{% endblock %}

# header
{% block breadcrumb %}{% endblock %}

# partial
{% extends 'skeleton_main' %}
{% block breadcrumb %} Breadcrumb {% endblock %}
{% block content %} Content {% endblock %}

Maybe there's something wrong with this approach?

2 Answers 2

3

You are using include which does not permit overriding blocks.

Is there a reason to use include instead of extend ?

Another solution would be to use embed which does the same function as include, but permits overriding blocks at the same time:

http://twig.sensiolabs.org/doc/tags/embed.html

3
  • Of course. I don't know, why I didn't think of embed before. Thanks, this works good.
    – lampshade
    Jun 29, 2015 at 9:18
  • I'm particularly unintelligent so a working example would be appreciated :)
    – Jeff
    Mar 27, 2018 at 21:56
  • The link no longer works - should be twig.symfony.com/doc/3.x/tags/embed.html now Nov 24, 2022 at 9:47
0

I think you have a wrong approch.

You should define header as a block, not as a separate template.

{# skeleton_main #}
{% block header %}
    {% block breadcrumb %}{% endblock %}
{% endblock %}
{% block content %}{% endblock %}

{# partial #}
{% extends 'skeleton_main' %}
{% block breadcrumb %} Breadcrumb {% endblock %}
{% block content %} Content {% endblock %}
1
  • I thought about that too. The header is very long, that's why I wanted it to be in another file, to make it easier to maintain. Using embed I can achieve this perfectly. Is there another reason, to not put the header into its own file?
    – lampshade
    Jun 29, 2015 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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