Skip to content
Myr

switching.rst

This file

Path

docs/switching.rst

226 lines · 6.9 KiB

Last changed by

use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509

History of this file

Take it

The walk stopped after 200 commits: lines older than 793ddd4 point at it.

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 1–6why, one step back
1Switching from other Template Engines
2=====================================34.. highlight:: html+jinja56If you have used a different template engine in the past and want to switch

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 7why, one step back
7to Jinja here is a small guide that shows the basic syntactic and semantic

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 8–9why, one step back
8changes between some common, similar text template engines for Python.
9

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 10–11why, one step back
10Jinja 1
11-------

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 12why, one step back
12

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 13–14why, one step back
13Jinja 2 is mostly compatible with Jinja 1 in terms of API usage and template
14syntax. The differences between Jinja 1 and 2 are explained in the following

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 15–20why, one step back
15list.
1617API18~~~1920Loaders

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 21why, one step back
21 Jinja 2 uses a different loader API. Because the internal representation

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 22–29why, one step back
22 of templates changed there is no longer support for external caching
23 systems such as memcached. The memory consumed by templates is comparable24 with regular Python modules now and external caching doesn't give any25 advantage. If you have used a custom loader in the past have a look at26 the new :ref:`loader API <loaders>`.2728Loading templates from strings29 In the past it was possible to generate templates from a string with the

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 30why, one step back
30 default environment configuration by using `jinja.from_string`. Jinja 2

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 31–34why, one step back
31 provides a :class:`Template` class that can be used to do the same, but
32 with optional additional configuration.3334Automatic unicode conversion

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 35why, one step back
35 Jinja 1 performed automatic conversion of bytestrings in a given encoding

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 36–37why, one step back
36 into unicode objects. This conversion is no longer implemented as it
37 was inconsistent as most libraries are using the regular Python ASCII

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 38–39why, one step back
38 bytestring to Unicode conversion. An application powered by Jinja 2
39 *has to* use unicode internally everywhere or make sure that Jinja 2 only

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 40–42why, one step back
40 gets unicode strings passed.
4142i18n

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 43–44why, one step back
43 Jinja 1 used custom translators for internationalization. i18n is now
44 available as Jinja 2 extension and uses a simpler, more gettext friendly

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 45–48why, one step back
45 interface and has support for babel. For more details see
46 :ref:`i18n-extension`.4748Internal methods

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 49why, one step back
49 Jinja 1 exposed a few internal methods on the environment object such

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 50why, one step back
50 as `call_function`, `get_attribute` and others. While they were marked

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 51why, one step back
51 as being an internal method it was possible to override them. Jinja 2

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 52–54why, one step back
52 doesn't have equivalent methods.
5354Sandbox

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 55–56why, one step back
55 Jinja 1 was running sandbox mode by default. Few applications actually
56 used that feature so it became optional in Jinja 2. For more details

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 57–59why, one step back
57 about the sandboxed execution see :class:`SandboxedEnvironment`.
5859Context

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 60–61why, one step back
60 Jinja 1 had a stacked context as storage for variables passed to the
61 environment. In Jinja 2 a similar object exists but it doesn't allow

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 62–72why, one step back
62 modifications nor is it a singleton. As inheritance is dynamic now
63 multiple context objects may exist during template evaluation.6465Filters and Tests66 Filters and tests are regular functions now. It's no longer necessary67 and allowed to use factory functions.686970Templates71~~~~~~~~~72

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 73why, one step back
73Jinja 2 has mostly the same syntax as Jinja 1. What's different is that

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 74–75why, one step back
74macros require parentheses around the argument list now.
75

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 76why, one step back
76Additionally Jinja 2 allows dynamic inheritance now and dynamic includes.

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 77–90why, one step back
77The old helper function `rendertemplate` is gone now, `include` can be used
78instead. Includes no longer import macros and variable assignments, for79that the new `import` tag is used. This concept is explained in the80:ref:`import` documentation.8182Another small change happened in the `for`-tag. The special loop variable83doesn't have a `parent` attribute, instead you have to alias the loop84yourself. See :ref:`accessing-the-parent-loop` for more details.858687Django88------8990If you have previously worked with Django templates, you should find

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 91why, one step back
91Jinja very familiar. In fact, most of the syntax elements look and

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 92–93why, one step back
92work the same.
93

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 94why, one step back
94However, Jinja provides some more syntax elements covered in the

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 95–121why, one step back
95documentation and some work a bit different.
9697This section covers the template changes. As the API is fundamentally98different we won't cover it here.99100Method Calls101~~~~~~~~~~~~102103In Django method calls work implicitly, while Jinja requires the explicit104Python syntax. Thus this Django code::105106 {% for page in user.get_created_pages %}107 ...108 {% endfor %}109110...looks like this in Jinja::111112 {% for page in user.get_created_pages() %}113 ...114 {% endfor %}115116This allows you to pass variables to the method, which is not possible in117Django. This syntax is also used for macros.118119Filter Arguments120~~~~~~~~~~~~~~~~121

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 122why, one step back
122Jinja provides more than one argument for filters. Also the syntax for

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 123–126why, one step back
123argument passing is different. A template that looks like this in Django::
124125 {{ items|join:", " }}126

use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509

line 127why, one step back
127looks like this in Jinja::

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 128–148why, one step back
128
129 {{ items|join(', ') }}130131It is a bit more verbose, but it allows different types of arguments -132including variables - and more than one of them.133134Tests135~~~~~136137In addition to filters there also are tests you can perform using the is138operator. Here are some examples::139140 {% if user.user_id is odd %}141 {{ user.username|e }} is odd142 {% else %}143 hmm. {{ user.username|e }} looks pretty normal144 {% endif %}145146Loops147~~~~~148

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 149why, one step back
149For loops work very similarly to Django, but notably the Jinja special

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 150–151why, one step back
150variable for the loop context is called `loop`, not `forloop` as in Django.
151

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 152why, one step back
152In addition, the Django `empty` argument is called `else` in Jinja. For

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 153–160why, one step back
153example, the Django template::
154155 {% for item in items %}156 {{ item }}157 {% empty %}158 No items!159 {% endfor %}160

use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509

line 161why, one step back
161...looks like this in Jinja::

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 162–171why, one step back
162
163 {% for item in items %}164 {{ item }}165 {% else %}166 No items!167 {% endfor %}168169Cycle170~~~~~171

use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509

line 172why, one step back
172The ``{% cycle %}`` tag does not exist in Jinja; however, you can achieve the

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 173–180why, one step back
173same output by using the `cycle` method on the loop context special variable.
174175The following Django template::176177 {% for user in users %}178 <li class="{% cycle 'odd' 'even' %}">{{ user }}</li>179 {% endfor %}180

use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509

line 181why, one step back
181...looks like this in Jinja::

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 182–194why, one step back
182
183 {% for user in users %}184 <li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li>185 {% endfor %}186187There is no equivalent of ``{% cycle ... as variable %}``.188189190Mako191----192193.. highlight:: html+mako194

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 195–196why, one step back
195If you have used Mako so far and want to switch to Jinja you can configure
196Jinja to look more like Mako:

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 197–201why, one step back
197
198.. sourcecode:: python199200 env = Environment('<%', '%>', '${', '}', '<%doc>', '</%doc>', '%', '##')201

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

lines 202–203why, one step back
202With an environment configured like that, Jinja should be able to interpret
203a small subset of Mako templates. Jinja does not support embedded Python

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

line 204why, one step back
204code, so you would have to move that out of the template. The syntax for defs

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 205why, one step back
205(which are called macros in Jinja) and template inheritance is different too.

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 206–215why, one step back
206The following Mako template::
207208 <%inherit file="layout.html" />209 <%def name="title()">Page Title</%def>210 <ul>211 % for item in list:212 <li>${item}</li>213 % endfor214 </ul>215

Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205

line 216why, one step back
216Looks like this in Jinja with the above configuration::

Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4

lines 217–226why, one step back
217
218 <% extends "layout.html" %>219 <% block title %>Page Title<% endblock %>220 <% block body %>221 <ul>222 % for item in list:223 <li>${item}</li>224 % endfor225 </ul>226 <% endblock %>