Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twig: can not override block in included file

Tags:

php

twig

symfony

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?

like image 774
user2538584 Avatar asked Sep 21 '13 09:09

user2538584


1 Answers

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

like image 56
Matt Marsh Avatar answered Nov 14 '22 04:11

Matt Marsh