OS : Linux
PHP Version : 7.4.33
Software : Apache/2.4.6 (CentOS) PHP/7.4.33
Information System : Linux apprendre2 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
Disable Function :
If you have used a different template engine in the past and want to switch
to Jinja2 here is a small guide that shows the basic syntatic and semantic
changes between some common, similar text template engines for Python. Jinja2 is mostly compatible with Jinja1 in terms of API usage and template
syntax. The differences between Jinja1 and 2 are explained in the following
list. Jinja2 has mostly the same syntax as Jinja1. What’s different is that
macros require parentheses around the argument list now. Additionally Jinja2 allows dynamic inheritance now and dynamic includes.
The old helper function rendertemplate is gone now, include can be used
instead. Includes no longer import macros and variable assignments, for
that the new import tag is used. This concept is explained in the
Import documentation. Another small change happened in the for-tag. The special loop variable
doesn’t have a parent attribute, instead you have to alias the loop
yourself. See Accessing the parent Loop for more details. If you have previously worked with Django templates, you should find
Jinja2 very familiar. In fact, most of the syntax elements look and
work the same. However, Jinja2 provides some more syntax elements covered in the
documentation and some work a bit different. This section covers the template changes. As the API is fundamentally
different we won’t cover it here. In Django method calls work implicitly. With Jinja2 you have to specify that
you want to call an object. Thus this Django code: will look like this in Jinja: This allows you to pass variables to the function which is also used for macros
which is not possible in Django. In Django you can use the following constructs to check for equality: In Jinja2 you can use the normal if statement in combination with operators: You can also have multiple elif branches in your template: Jinja2 provides more than one argument for filters. Also the syntax for
argument passing is different. A template that looks like this in Django: looks like this in Jinja2: In fact it’s a bit more verbose but it allows different types of arguments -
including variables - and more than one of them. In addition to filters there also are tests you can perform using the is
operator. Here are some examples: For loops work very similar to Django. Notably, in Jinja2 the special variable for
the loop context is called loop and not forloop like in Django. In addition, the Django empty argument is called else in Jinja2. For example, the
Django template: would be handled in Jinja2 as: The {% cycle %} tag does not exist in Jinja because of it’s implicit
nature. However you can achieve mostly the same by using the cycle
method on a loop object. The following Django template: Would look like this in Jinja: There is no equivalent of {% cycle ... as variable %}. If you have used Mako so far and want to switch to Jinja2 you can configure
Jinja2 to look more like Mako: Once the environment is configured like that Jinja2 should be able to interpret
a small subset of Mako templates. Jinja2 does not support embedded Python code
so you would have to move that out of the template. The syntax for defs (in
Jinja2 defs are called macros) and template inheritance is different too. The
following Mako template: Looks like this in Jinja2 with the above configuration:Switching from other Template Engines¶
Jinja1¶
API¶
Templates¶
Django¶
Method Calls¶
{% for page in user.get_created_pages %}
...
{% endfor %}
{% for page in user.get_created_pages() %}
...
{% endfor %}
Conditions¶
{% ifequal foo "bar" %}
...
{% else %}
...
{% endifequal %}
{% if foo == 'bar' %}
...
{% else %}
...
{% endif %}
{% if something %}
...
{% elif otherthing %}
...
{% elif foothing %}
...
{% else %}
...
{% endif %}
Filter Arguments¶
{{ items|join:", " }}
{{ items|join(', ') }}
Tests¶
{% if user.user_id is odd %}
{{ user.username|e }} is odd
{% else %}
hmm. {{ user.username|e }} looks pretty normal
{% endif %}
Loops¶
{% for item in items %}
{{ item }}
{% empty %}
No items!
{% endfor %}
{% for item in items %}
{{ item }}
{% else %}
No items!
{% endfor %}
Cycle¶
{% for user in users %}
<li class="{% cycle 'odd' 'even' %}">{{ user }}</li>
{% endfor %}
{% for user in users %}
<li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li>
{% endfor %}
Mako¶
env = Environment('<%', '%>', '${', '}', '<%doc>', '</%doc>', '%', '##')
<%inherit file="layout.html" />
<%def name="title()">Page Title</%def>
<ul>
% for item in list:
<li>${item}</li>
% endfor
</ul>
<% extends "layout.html" %>
<% block title %>Page Title<% endblock %>
<% block body %>
<ul>
% for item in list:
<li>${item}</li>
% endfor
</ul>
<% endblock %>