Here's a basic example of using nested extends
in Twig. I figured this is a probably common use-case. You have a very general _layout.html
that contains your common CSS, JS, meta tags, etc.
{# _layout.html #}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Then you have a _subpage.html
template that shapes the way all your subpages should look (main content area, sidebar, etc).
{# _subpage.html #}
{% extends "_layout" %}
{% block content %}
<main>
{% block subpageContent %}{% endblock %}
</main>
<aside>
{% block subpageSidebar %}{% endblock %}
</aside>
{% endblock %}
Now I extend that _subpage.html
in about.html
and grab the blocks that belong to the _subpage.html
template.
{# about.html #}
{% extends "_subpage" %}
{% set title = "About" %}
{% block subpageContent %}
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae cupiditate suscipit, debitis accusantium tenetur impedit, veritatis id placeat, dolor hic temporibus architecto quas atque sunt officiis corrupti voluptatibus expedita. Assumenda!</p>
{% endblock %}
{% block subpageSidebar %}
<li><a href="#">Subpage Link 1</a></li>
<li><a href="#">Subpage Link 2</a></li>
<li><a href="#">Subpage Link 3</a></li>
{% endblock %}
Now I realize you can use nested extends in Twig, but I'm entirely sure if I should. Is this a maintenance nightmare? Does it take a performance hit to do this? What's the best practice for template relationships?