I'm not sure if what I want to do is possible: I'm trying to get a block in a parent template to be filled out by a file included in a child template of the parent.
The best way to explain this is a test case:
File t1.djhtml:
<root>
<block t3_container>
{% block t3 %}This should be 'CONTENT'{% endblock %}
</block t3_container>
<block t2_container>
{% block t2 %}{% endblock %}
</block t2_container>
</root>
File t2.djhtml:
{% extends 't1.djhtml' %}
{% block t2 %}
<block t2>
{%- include 't3.djhtml' with context %}
</block t2>
{% endblock %}
File t3.djhtml:
{% block t3 %}
<block t3>
CONTENT
</block t3>
{% endblock %}
File test.py:
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader(''))
t=env.get_template('t2.djhtml')
print t.render()
The output is:
<root>
<block t3_container>
This should be 'CONTENT'
</block t3_container>
<block t2_container>
<block t2>
<block t3>
CONTENT
</block t3>
</block t2>
</block t2_container>
</root>
The t2
block should be empty, and t3_container
should have block t3
's content inside. How do I accomplish this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…