clean up engine comparisons
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent ec50af3 · Browse files · 1 file·+78 added−123 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+78 added−123 removed
- other:+78 added−123 removed
Provenance
- Commit
9de99f8- Tree
6f9f09c- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
ec50af3
1 file changed+78 added−123 removed
modified:docs/switching.rst+78 added-123 removed
1-Switching from other Template Engines1+Switching From Other Template Engines2 =====================================3 4-.. highlight:: html+jinja4+This is a brief guide on some of the differences between Jinja syntax5+and other template languages. See :doc:`/templates` for a comprehensive6+guide to Jinja syntax and features.7 6-If you have used a different template engine in the past and want to switch7-to Jinja here is a small guide that shows the basic syntactic and semantic8-changes between some common, similar text template engines for Python.8 10-Jinja 111--------12-13-Jinja 2 is mostly compatible with Jinja 1 in terms of API usage and template14-syntax. The differences between Jinja 1 and 2 are explained in the following15-list.16-17-API18-~~~19-20-Loaders21- Jinja 2 uses a different loader API. Because the internal representation22- 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>`.27-28-Loading templates from strings29- In the past it was possible to generate templates from a string with the30- default environment configuration by using `jinja.from_string`. Jinja 231- provides a :class:`Template` class that can be used to do the same, but32- with optional additional configuration.33-34-Automatic unicode conversion35- Jinja 1 performed automatic conversion of bytes in a given encoding36- into unicode objects. This conversion is no longer implemented as it37- was inconsistent as most libraries are using the regular Python38- ASCII bytes to Unicode conversion. An application powered by Jinja 239- *has to* use unicode internally everywhere or make sure that Jinja 240- only gets unicode strings passed.41-42-i18n43- Jinja 1 used custom translators for internationalization. i18n is now44- available as Jinja 2 extension and uses a simpler, more gettext friendly45- interface and has support for babel. For more details see46- :ref:`i18n-extension`.47-48-Internal methods49- Jinja 1 exposed a few internal methods on the environment object such50- as `call_function`, `get_attribute` and others. While they were marked51- as being an internal method it was possible to override them. Jinja 252- doesn't have equivalent methods.53-54-Sandbox55- Jinja 1 was running sandbox mode by default. Few applications actually56- used that feature so it became optional in Jinja 2. For more details57- about the sandboxed execution see :class:`SandboxedEnvironment`.58-59-Context60- 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 allow62- modifications nor is it a singleton. As inheritance is dynamic now63- multiple context objects may exist during template evaluation.64-65-Filters and Tests66- Filters and tests are regular functions now. It's no longer necessary67- and allowed to use factory functions.68-69-70-Templates71-~~~~~~~~~72-73-Jinja 2 has mostly the same syntax as Jinja 1. What's different is that74-macros require parentheses around the argument list now.75-76-Additionally Jinja 2 allows dynamic inheritance now and dynamic includes.77-The old helper function `rendertemplate` is gone now, `include` can be used78-instead. Includes no longer import macros and variable assignments, for79-that the new `import` tag is used. This concept is explained in the80-:ref:`import` documentation.81-82-Another small change happened in the `for`-tag. The special loop variable83-doesn't have a `parent` attribute, instead you have to alias the loop84-yourself. See :ref:`accessing-the-parent-loop` for more details.85-86-9 Django10 ------11 12 If you have previously worked with Django templates, you should find91-Jinja very familiar. In fact, most of the syntax elements look and92-work the same.13+Jinja very familiar. Many of the syntax elements look and work the same.14+However, Jinja provides some more syntax elements, and some work a bit15+differently.16 94-However, Jinja provides some more syntax elements covered in the95-documentation and some work a bit different.17+This section covers the template changes. The API, including extension18+support, is fundamentally different so it won't be covered here.19 97-This section covers the template changes. As the API is fundamentally98-different we won't cover it here.20+Django supports using Jinja as its template engine, see21+https://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines.22 23+24 Method Calls25 ~~~~~~~~~~~~26 103-In Django method calls work implicitly, while Jinja requires the explicit104-Python syntax. Thus this Django code::27+In Django, methods are called implicitly, without parentheses.28 29+.. code-block:: django30+31 {% for page in user.get_created_pages %}32 ...33 {% endfor %}34 110-...looks like this in Jinja::35+In Jinja, using parentheses is required for calls, like in Python. This36+allows you to pass variables to the method, which is not possible37+in Django. This syntax is also used for calling macros.38 39+.. code-block:: jinja40+41 {% for page in user.get_created_pages() %}42 ...43 {% endfor %}44 116-This allows you to pass variables to the method, which is not possible in117-Django. This syntax is also used for macros.45 46 Filter Arguments47 ~~~~~~~~~~~~~~~~48 122-Jinja provides more than one argument for filters. Also the syntax for123-argument passing is different. A template that looks like this in Django::49+In Django, one literal value can be passed to a filter after a colon.50 51+.. code-block:: django52+53 {{ items|join:", " }}54 127-looks like this in Jinja::55+In Jinja, filters can take any number of positional and keyword56+arguments in parentheses, like function calls. Arguments can also be57+variables instead of literal values.58 129- {{ items|join(', ') }}59+.. code-block:: jinja60 131-It is a bit more verbose, but it allows different types of arguments -132-including variables - and more than one of them.61+ {{ items|join(", ") }}62 63+64 Tests65 ~~~~~66 137-In addition to filters there also are tests you can perform using the is138-operator. Here are some examples::67+In addition to filters, Jinja also has "tests" used with the ``is``68+operator. This operator is not the same as the Python operator.69 70+.. code-block:: jinja71+72 {% if user.user_id is odd %}73 {{ user.username|e }} is odd74 {% else %}75 hmm. {{ user.username|e }} looks pretty normal1 unmodified line77 78 Loops79 ~~~~~80 149-For loops work very similarly to Django, but notably the Jinja special150-variable for the loop context is called `loop`, not `forloop` as in Django.81+In Django, the special variable for the loop context is called82+``forloop``, and the ``empty`` is used for no loop items.83 152-In addition, the Django `empty` argument is called `else` in Jinja. For153-example, the Django template::84+.. code-block:: django85 86 {% for item in items %}87 {{ item }}88 {% empty %}89 No items!90 {% endfor %}91 161-...looks like this in Jinja::92+In Jinja, the special variable for the loop context is called ``loop``,93+and the ``else`` block is used for no loop items.94 95+.. code-block:: jinja96+97 {% for item in items %}164- {{ item }}98+ {{ loop.index}}. {{ item }}99 {% else %}100 No items!101 {% endfor %}102 103+104 Cycle105 ~~~~~106 172-The ``{% cycle %}`` tag does not exist in Jinja; however, you can achieve the173-same output by using the `cycle` method on the loop context special variable.107+In Django, the ``{% cycle %}`` can be used in a for loop to alternate108+between values per loop.109 175-The following Django template::110+.. code-block:: django111 112 {% for user in users %}113 <li class="{% cycle 'odd' 'even' %}">{{ user }}</li>114 {% endfor %}115 181-...looks like this in Jinja::116+In Jinja, the ``loop`` context has a ``cycle`` method.117 118+.. code-block:: jinja119+120 {% for user in users %}121 <li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li>122 {% endfor %}123 187-There is no equivalent of ``{% cycle ... as variable %}``.124+A cycler can also be assigned to a variable and used outside or across125+loops with the ``cycle()`` global function.126 127 128 Mako129 ----130 193-.. highlight:: html+mako131+You can configure Jinja to look more like Mako:132 195-If you have used Mako so far and want to switch to Jinja you can configure196-Jinja to look more like Mako:133+.. code-block:: python134 198-.. sourcecode:: python135+ env = Environment(136+ block_start_string="<%",137+ block_end_string="%>",138+ variable_start_string="${",139+ variable_end_string="}",140+ comment_start_string="<%doc>",141+ commend_end_string="</%doc>",142+ line_statement_prefix="%",143+ line_comment_prefix="##",144+ )145 200- env = Environment('<%', '%>', '${', '}', '<%doc>', '</%doc>', '%', '##')146+With an environment configured like that, Jinja should be able to147+interpret a small subset of Mako templates without any changes.148 202-With an environment configured like that, Jinja should be able to interpret203-a small subset of Mako templates. Jinja does not support embedded Python204-code, so you would have to move that out of the template. The syntax for defs205-(which are called macros in Jinja) and template inheritance is different too.206-The following Mako template::149+Jinja does not support embedded Python code, so you would have to move150+that out of the template. You could either process the data with the151+same code before rendering, or add a global function or filter to the152+Jinja environment.153 154+The syntax for defs (which are called macros in Jinja) and template155+inheritance is different too.156+157+The following Mako template:158+159+.. code-block:: mako160+161 <%inherit file="layout.html" />162 <%def name="title()">Page Title</%def>163 <ul>164 % for item in list:165 <li>${item}</li>166 % endfor167 </ul>168 216-Looks like this in Jinja with the above configuration::169+Looks like this in Jinja with the above configuration:170 171+.. code-block:: jinja172+173 <% extends "layout.html" %>174 <% block title %>Page Title<% endblock %>175 <% block body %>176 <ul>5 unmodified lines