Twig
Twig copied to clipboard
[Feature] - {{ parent($level) }} with level
Feature :
Call {{ parent($level) }} with level when having multiple level of extends, closest levels are ignored (like continue and break in php)
Ex:
{{ parent(2) }}
Example (breadcrumbs) :
// base.htm.twig
<ol class="breadcrumb">
{% block breadcrumbs %}
<li class="breadcrumb-item active"><a href="{{ path("home") }}">Accueil</a></li>
{% endblock %}
</ol>
// missions.htm.twig
{% extends 'base.html.twig' %}
{% block breadcrumbs %}
{{ parent() }}
<li class="breadcrumb-item active"><a>Missions</a></li>
{% endblock %}
// mission_details.htm.twig
{% extends 'missions.html.twig' %}
{% block breadcrumbs %}
{{ parent(2) }}
<li class="breadcrumb-item active"><a href="{{ path('mission') }}">Missions</a></li>
<li class="breadcrumb-item active"><a>Mission {{ mission.title }}</a></li>
<li class="breadcrumb-item active"><a>Details</a></li>
{% endblock %}
This feature would allow me to override symfony forms, without adding the parent block manually.
To me, comparing this to break or continue of PHP as an argument to justify the feature is flawed.
The closest concept that can be used in PHP to think about blocks would be class methods (I generally think about them as protected methods, but the ability to render blocks from arbitrary templates with the second argument of block() would rather make them public methods).
As a side note, in the early versions of Twig (pre-1.0) that did not have traits ({% use %}) or dynamic inheritance, blocks were actually compiled into protected methods of the compiled template (and Twig inheritance was compiled into PHP inheritance).
There, you cannot do parent<2>::breadcrumbs() or whatever other syntax you imagine for that.
We already support {{ block('breadcrumbs', 'base.htm.twig') }} to render the block of the grandparent