user2538584
user2538584

Reputation: 541

twig: can not override block in included file

How can I override a block inside an included template file?

example:

{# layout.html #}
{% include "menu.html" %}

{# menu.html #}
{% block overrideme %}{% endblock %}

{# index.html #}
{% extends "layout.html" %}
{% block overrideme %}Overriden{% endblock %}

I read somewhere that a trait function was implemented? I can't find any documentation about it though, does anyone know how I could make this work?

Upvotes: 17

Views: 9239

Answers (1)

Matt Marsh
Matt Marsh

Reputation: 266

If you want to override blocks inside a file that you are including then you should 'embed' it rather than 'include' it.

{% embed "menu.html" %}
    {% block overrideme %}
        Overriden
    {% endblock %}
{% endembed %}

See the docs for more details: http://twig.sensiolabs.org/doc/tags/embed.html

Upvotes: 23

Related Questions