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 fileTake 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 back1Switching from other Template Engines2=====================================34.. highlight:: html+jinja56If you have used a different template engine in the past and want to switchMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 7why, one step back7to Jinja here is a small guide that shows the basic syntactic and semanticMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 8–9why, one step back8changes between some common, similar text template engines for Python.9Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 10–11why, one step back10Jinja 111-------Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
line 12why, one step back12Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 13–14why, one step back13Jinja 2 is mostly compatible with Jinja 1 in terms of API usage and template14syntax. The differences between Jinja 1 and 2 are explained in the followingMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 15–20why, one step back15list.1617API18~~~1920LoadersMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 21why, one step back21 Jinja 2 uses a different loader API. Because the internal representationMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 22–29why, one step back22 of templates changed there is no longer support for external caching23 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 theMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 30why, one step back30 default environment configuration by using `jinja.from_string`. Jinja 2Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 31–34why, one step back31 provides a :class:`Template` class that can be used to do the same, but32 with optional additional configuration.3334Automatic unicode conversionMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 35why, one step back35 Jinja 1 performed automatic conversion of bytestrings in a given encodingMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 36–37why, one step back36 into unicode objects. This conversion is no longer implemented as it37 was inconsistent as most libraries are using the regular Python ASCIIMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 38–39why, one step back38 bytestring to Unicode conversion. An application powered by Jinja 239 *has to* use unicode internally everywhere or make sure that Jinja 2 onlyMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 40–42why, one step back40 gets unicode strings passed.4142i18nMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 43–44why, one step back43 Jinja 1 used custom translators for internationalization. i18n is now44 available as Jinja 2 extension and uses a simpler, more gettext friendlyMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 45–48why, one step back45 interface and has support for babel. For more details see46 :ref:`i18n-extension`.4748Internal methodsMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 49why, one step back49 Jinja 1 exposed a few internal methods on the environment object suchMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
line 50why, one step back50 as `call_function`, `get_attribute` and others. While they were markedMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 51why, one step back51 as being an internal method it was possible to override them. Jinja 2Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 52–54why, one step back52 doesn't have equivalent methods.5354SandboxMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 55–56why, one step back55 Jinja 1 was running sandbox mode by default. Few applications actually56 used that feature so it became optional in Jinja 2. For more detailsMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 57–59why, one step back57 about the sandboxed execution see :class:`SandboxedEnvironment`.5859ContextMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 60–61why, one step back60 Jinja 1 had a stacked context as storage for variables passed to the61 environment. In Jinja 2 a similar object exists but it doesn't allowMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 62–72why, one step back62 modifications nor is it a singleton. As inheritance is dynamic now63 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~~~~~~~~~72Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 73why, one step back73Jinja 2 has mostly the same syntax as Jinja 1. What's different is thatMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 74–75why, one step back74macros require parentheses around the argument list now.75Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 76why, one step back76Additionally Jinja 2 allows dynamic inheritance now and dynamic includes.Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 77–90why, one step back77The old helper function `rendertemplate` is gone now, `include` can be used78instead. 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 findMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 91why, one step back91Jinja very familiar. In fact, most of the syntax elements look andMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 92–93why, one step back92work the same.93Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 94why, one step back94However, Jinja provides some more syntax elements covered in theMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 95–121why, one step back95documentation 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~~~~~~~~~~~~~~~~121Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 122why, one step back122Jinja provides more than one argument for filters. Also the syntax forMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 123–126why, one step back123argument passing is different. A template that looks like this in Django::124125 {{ items|join:", " }}126use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509
line 127why, one step back127looks like this in Jinja::Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 128–148why, one step back128129 {{ 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~~~~~148Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 149why, one step back149For loops work very similarly to Django, but notably the Jinja specialMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 150–151why, one step back150variable for the loop context is called `loop`, not `forloop` as in Django.151Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 152why, one step back152In addition, the Django `empty` argument is called `else` in Jinja. ForMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 153–160why, one step back153example, the Django template::154155 {% for item in items %}156 {{ item }}157 {% empty %}158 No items!159 {% endfor %}160use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509
line 161why, one step back161...looks like this in Jinja::Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 162–171why, one step back162163 {% for item in items %}164 {{ item }}165 {% else %}166 No items!167 {% endfor %}168169Cycle170~~~~~171use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509
line 172why, one step back172The ``{% cycle %}`` tag does not exist in Jinja; however, you can achieve theMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 173–180why, one step back173same 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 %}180use "Jinja" instead of "Jinja2"David Lordbefore adoption8182509
line 181why, one step back181...looks like this in Jinja::Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 182–194why, one step back182183 {% 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+mako194Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 195–196why, one step back195If you have used Mako so far and want to switch to Jinja you can configure196Jinja to look more like Mako:Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 197–201why, one step back197198.. sourcecode:: python199200 env = Environment('<%', '%>', '${', '}', '<%doc>', '</%doc>', '%', '##')201Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
lines 202–203why, one step back202With an environment configured like that, Jinja should be able to interpret203a small subset of Mako templates. Jinja does not support embedded PythonMerge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
line 204why, one step back204code, so you would have to move that out of the template. The syntax for defsMerge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 205why, one step back205(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 back206The 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>215Merge pull request #1087 from pallets/jinja-nameDavid Lordbefore adoption1af1205
line 216why, one step back216Looks like this in Jinja with the above configuration::Merge branch '2.9-maintenance'Armin Ronacherbefore adoption793ddd4
lines 217–226why, one step back217218 <% 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 %>