

Partially render in Jinja
source link: https://www.chunyangwen.com/blog/python/jinja-partial-template.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Partially render in Jinja
Posted by chunyang on June 7, 2022A Jinja template can be rendered. Sometimes the data we need can not be collected in just one place. It will be helpful if we can render a template multiple times.
By default, a Jinja template can not be rendered multiple times. It will raise an exception or insert null values into the corresponding placeholders.
In the blog, we will use a trick to mimic the partially rendering behavior. Jinja has an
operator raw
which can be used to render its original content.
from jinja2 import Template
template_str = """
{% raw %} {% raw %}
{{hi}}
{% endraw %}
"""
partial_template_str = Template(template_str).render()
result = Template(partial_template_str).render(hi=3)
But using raw
will introduce redundant newlines. We can not use -
to remove the spaces.
It is wrong sometimes we remove the leading spaces.
from jinja2 import Template
template_str = """
{% raw %}
{{hi}}
{% endraw %}
"""
partial_template_str = Template(template_str, trim_blocks=True, lstrip_blocks=True).render()
result = Template(partial_template_str).render(hi=3)
print(result)
If the leading spaces are not meaningless to your program, you can use -
to remove them. Referecne
from jinja2 import Template
template_str = """
{% raw -%}
{{hi}}
{% endraw %}
"""
partial_template_str = Template(template_str, trim_blocks=True, lstrip_blocks=True).render()
result = Template(partial_template_str).render(hi=3)
print(result)
If we need more nested templates, such as we want a template to be rendered three or more times, the code will be ugly. It seems that Jinja does not provide way to implement a customized operator
from jinja2 import Template
template_str = """
{% raw -%}
{{hi}}
{% endraw -%}
{{'{%'}} raw {{'%}'}}
{{hello}}
{{'{%'}} endraw {{'%}'}}
"""
partial_template_str = Template(template_str, trim_blocks=True, lstrip_blocks=True).render()
print(partial_template_str)
print("=" * 10)
result = Template(partial_template_str).render(hi=3)
print(result)
print("=" * 10)
final_result = Template(result).render(hello=4)
print(final_result)
raw
and endraw
can not be nested. You are not allowed to insert raw
and
endraw
block in another raw
and endraw
block`.
Recommend
-
96
Start WritingLog inNotificationsThe Cybersecurity Writing Contest by Twingate and HackerNoonLast Wednesd...
-
40
I wrote git-format-staged to apply an automatic code formatter or linter to staged files. It ignores unstaged changes, and leaves those changes unstaged. Whe...
-
63
The PALM tree algorithm for partially additive (generalized) linear model trees is introduced along with the R package palmtree. One potential application is modeling of treatment-subgroup interactions while adjusting for...
-
53
Authors: Jon Gjengset, Malte Schwarzkopf, Jonathan Behrens, and Lara Timbó Araújo, MIT CSAIL; Martin Ek, Norwegian University of Science and Technology; Eddie Kohler, Harvard University;
-
30
The FuzzyLog: a partially ordered shared log Lockerman et al., OSDI’18 If you want to build a distributed system then havi...
-
13
如何向Jinja宏传递额外参数(*args和**kwargs)? 发表回复 这段时间有多个读者问关于Jinja宏定义时的参数接受问题。这一点在
-
13
Files Permalink Latest commit message Commit time
-
5
-
4
Primer on Jinja Te...
-
7
In this article, we will guide you step-by-step on How to generate sitemaps for SEO using Jinja templates in Python. Emphasizing the critical importance of a...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK