Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use blocks from included files for parent in jinja2

Tags:

python

jinja2

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?

like image 365
Richard Avatar asked Mar 08 '12 21:03

Richard


People also ask

What is block content in Jinja?

Jinja allows you to put the name of the block after the end tag for better readability: {% block sidebar %} {% block inner_sidebar %} ... {% endblock inner_sidebar %} {% endblock sidebar %} However, the name after the endblock word must match the block name.

Which three features are included in the Jinja2 templates?

Some of the features of Jinja are: sandboxed execution. automatic HTML escaping to prevent cross-site scripting (XSS) attacks. template inheritance.

What does super () do in Jinja?

So you want to add a design for all pages in your app? Instead of copying and pasting the same code throughout all your files, you can use a super block. There is an easier way, instead you can use a super block.


1 Answers

You can use macros in the included file, but instead of including it, you import the macros with context.


T1.html

<root>
  <block t3_container>
    {% block t3 %}{% endblock %}
  </block t3_container>

  <block t2_container>
  {% block t2 %}{% endblock %}
  </block t2_container>
</root>

T2.html

{% extends 'T1.html' %}
{%- from 'T3.html' import inner, inner2 with context %}

{% block t3 %}
    {{ inner2() }}   
{% endblock %}

{% block t2 %}
    <block t2>
        {{ inner() }}
    </block t2>
{% endblock %}

T3.html

{% macro inner2() %}
    <block t3>
        CONTENT '{{ foo+1 }}'
    </block t3>
{% endmacro %}

{% macro inner() %}
  hello
{% endmacro %}

test.py

from jinja2 import Environment, FileSystemLoader

env = Environment(loader=FileSystemLoader("."))
t = env.get_template("T2.html")

print(t.render({"foo": 122}))
like image 163
Richard Avatar answered Oct 10 '22 22:10

Richard