8000 Improve the overriding of bundle templates · Issue #17557 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Improve the overriding of bundle templates #17557
Closed
@javiereguiluz

Description

@javiereguiluz

This feature has been asked by lots and lots of users in #15755, #17054 and #17407.

Problem

  1. You have installed MyBundle which contains this template:
{# MyBundle/Resources/views/base.html.twig #}
{% block title '...' %}

{% block content %}
    {# a lot of contents here #}
{% endblock %}
  1. You override that template in your app by creating app/Resources/MyBundle/views/base.html.twig.

  2. In your own template, you want to override just the title block and reuse the original content 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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0