I'm trying to figure out the best way to standardize on a block for all
templates ( {% block content %} ) but allow for being able to drop in a
layer between the base template and the outermost template. The use case
would be for a blog site where the static pages all extend base.twig but
for certain classes of pages you might want to wrap content in a consistent
way, like for posts. What I'm doing in my base.twig currently is:
{% block content_wrapper %}{% block content %}{% endblock %}{% endblock %}
All of my edge templates can just use {% block content %} to inject their
content, and can either extend base.twig or something like post.twig. In
that case, post.twig looks like:
{% block content_wrapper %}<div class="custom-things post-stuff">{% block
content %}{% endblock %}</div>{% endblock %}
Which is fine, but doesn't feel very elegant. I thought it would be
something like parent, but child. But given how this is all based on PHP
classes, I don't really know if I'd know how to do this with objects
directly so maybe it isn't possible? Is there a way to do something like
this natively that I completely missed?
Here is my naive expectation on how this should work. I understand that it
doesn't, but this is the first thing I tried before I really thought about
what I was doing. :) It might better show what I'm hoping to accomplish.
*base.twig*
<html>
{% block content %}{% endblock %}
</html>
*post.twig*
{% extend "base.twig" %}
{% block content %}
<div class="post">
<h1>{{ title }}</h1>
{% block content %}{% endblock %}
{% endblock %}
*index.twig*
{% extend "base.twig" %}
{% block content %}
Index Content.
{% endblock %}
*some-post.twig*
{% extend "post.twig" %}
{% set title = 'This is Some Post' %}
{% block content %}
This is Some Post content.
{% endblock %}
*index.html (rendered index.twig)*
<html>
Index Content.
</html>
*some-post.html (rendered some-post.twig)*
<html>
<div class="post">
<h1>This is Some Post</h1>
This is Some Post content.
</html>