Block re-override behavior in multi-level inheritance #51
SeanTAllen
started this conversation in
Research
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Template inheritance (#50) resolves block overrides by inlining the override content directly, stripping the
_Blockwrapper. This means once a block is overridden at one level, the "slot" disappears from the AST and deeper descendants can't re-override it.Given a three-level chain:
The child's override of
contenthas no effect because the parent already consumed that block — there's no_Block("content")node left in the resolved parent AST for the child to target.A parent that wants to allow re-override would need to explicitly re-expose the slot:
This wraps the parent's override in a new block of the same name, preserving the slot for the child. It works today but it's not obvious that you'd need to do this.
Jinja2/Django comparison
Jinja2 and Django automatically wrap each override in a new block of the same name, so every level can re-override. They also provide
{{ super() }}to include the parent's content within an override. Our current design omits both of these.Questions for future consideration
Should overrides automatically preserve the slot? This would mean wrapping override content in a
_Blockof the same name during_apply_overrides, so every level can re-override without the explicit double-wrap trick. The downside is slightly more complex resolution logic and potentially surprising behavior if someone doesn't expect the slot to persist.Should we add
{{ super() }}? This would let a child include the parent's block content within its override, which is the most common use of re-overridable blocks in Jinja2/Django. Addingsuper()without automatic slot preservation would be less useful since the slot disappears after one override.Is the current behavior sufficient? The explicit double-wrap works today. Multi-level inheritance where the same block is overridden at every level may not be a common pattern for this library's use cases.
No action needed now. This is documenting the design space for when (if) users ask for deeper inheritance behavior.
All reactions