DivineSlayer
DivineSlayer

Reputation: 339

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?

Upvotes: 5

Views: 705

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239470

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.

Upvotes: 4

Related Questions