Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass blocks into an included template without extending

I would like to know the best way to include a template many times throughout a project and possibly many times within a parent template. This included child template is just a skeleton that must be filled with both variables and blocks of html. Example templates:

parent.html:

<p>Popup #1</p>
{% include "popup.html" %}
<p>Popup #2</p>
{% include "popup.html" %}

popup.html:

<h1 class="title">{% block title %}{% endblock %}</h1>
<div class="body">{% block body %}{% endblock %}</div>

The only solution i can think of is to have a separate file for every popup and extend popup.html in each file. Then, I could include these extended files into my parent. This requires adding a file for every popup which would be frustrating. What would be the best way to accomplish this?

like image 960
DivineSlayer Avatar asked Oct 08 '22 15:10

DivineSlayer


1 Answers

The Django template engine falls down in this area. There's no way to include files and parse blocks in them. You can pass data into the include, allowing you do something like:

<h1 class="title">{{ title }}</h1>
<div class="body">{{ body }}</div>

But if you need actual blocks, they have to be directly in the main template file, or one of its parents.

like image 97
Chris Pratt Avatar answered Oct 12 '22 02:10

Chris Pratt