Description
This feature has been asked by lots and lots of users in #15755, #17054 and #17407.
Problem
- You have installed
MyBundle
which contains this template:
{# MyBundle/Resources/views/base.html.twig #}
{% block title '...' %}
{% block content %}
{# a lot of contents here #}
{% endblock %}
-
You override that template in your app by creating
app/Resources/MyBundle/views/base.html.twig
. -
In your own template, you want to override just the
title
block and reuse the originalcontent
block.
{# app/Resources/MyBundle/views/base.html.twig #}
{% extends 'MyBundle::base.html.twig' %}
{% block title 'New title' %}
This won't work and Symfony will show a "reached nested level" error. The only current solution is to copy+paste the entire original template (and keep it in sync forever if the bundle changes it).
Solution
People asking for this feature suggest to add a new notation to tell Twig/Symfony that you want to override the original template, not your copy:
{# app/Resources/MyBundle/views/base.html.twig #}
{% extends '!MyBundle::base.html.twig' %}
{% block title 'New title' %}
Some people (e.g. @jakzal) don't like the new notation. The problem is that !
always mean "not", so it can be read as "don't extend ...".
Since Twig prefers expressive syntax, maybe we could use a word to enable the special extends
behavior?
{# app/Resources/MyBundle/views/base.html.twig #}
{% extends original 'MyBundle::base.html.twig' %}
{% block title 'New title' %}
Comments? Thoughts? Better ideas? Thanks!