lampshade
lampshade

Reputation: 2786

How to pass a block to an included template?

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?

Upvotes: 1

Views: 954

Answers (2)

Yassine Guedidi
Yassine Guedidi

Reputation: 1715

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 %}

Upvotes: 0

Dric512
Dric512

Reputation: 3729

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

Upvotes: 3

Related Questions