Skip to content
Myr

switching.rst

This file

Path

docs/switching.rst

181 lines · 4.3 KiB

Last changed by

Merge branch '3.1.x'David Lordbefore adoption5d9ece6

History of this file

Take it

1Switching From Other Template Engines2=====================================34This is a brief guide on some of the differences between Jinja syntax5and other template languages. See :doc:`/templates` for a comprehensive6guide to Jinja syntax and features.789Django10------1112If you have previously worked with Django templates, you should find13Jinja very familiar. Many of the syntax elements look and work the same.14However, Jinja provides some more syntax elements, and some work a bit15differently.1617This section covers the template changes. The API, including extension18support, is fundamentally different so it won't be covered here.1920Django supports using Jinja as its template engine, see21https://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines.222324Method Calls25~~~~~~~~~~~~2627In Django, methods are called implicitly, without parentheses.2829.. code-block:: django3031    {% for page in user.get_created_pages %}32        ...33    {% endfor %}3435In Jinja, using parentheses is required for calls, like in Python. This36allows you to pass variables to the method, which is not possible37in Django. This syntax is also used for calling macros.3839.. code-block:: jinja4041    {% for page in user.get_created_pages() %}42        ...43    {% endfor %}444546Filter Arguments47~~~~~~~~~~~~~~~~4849In Django, one literal value can be passed to a filter after a colon.5051.. code-block:: django5253    {{ items|join:", " }}5455In Jinja, filters can take any number of positional and keyword56arguments in parentheses, like function calls. Arguments can also be57variables instead of literal values.5859.. code-block:: jinja6061    {{ items|join(", ") }}626364Tests65~~~~~6667In addition to filters, Jinja also has "tests" used with the ``is``68operator. This operator is not the same as the Python operator.6970.. code-block:: jinja7172    {% if user.user_id is odd %}73        {{ user.username|e }} is odd74    {% else %}75        hmm. {{ user.username|e }} looks pretty normal76    {% endif %}7778Loops79~~~~~8081In Django, the special variable for the loop context is called82``forloop``, and the ``empty`` is used for no loop items.8384.. code-block:: django8586    {% for item in items %}87        {{ forloop.counter }}. {{ item }}88    {% empty %}89        No items!90    {% endfor %}9192In Jinja, the special variable for the loop context is called ``loop``,93and the ``else`` block is used for no loop items.9495.. code-block:: jinja9697    {% for item in items %}98        {{ loop.index }}. {{ item }}99    {% else %}100        No items!101    {% endfor %}102103104Cycle105~~~~~106107In Django, the ``{% cycle %}`` can be used in a for loop to alternate108between values per loop.109110.. code-block:: django111112    {% for user in users %}113        <li class="{% cycle 'odd' 'even' %}">{{ user }}</li>114    {% endfor %}115116In Jinja, the ``loop`` context has a ``cycle`` method.117118.. code-block:: jinja119120    {% for user in users %}121        <li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li>122    {% endfor %}123124A cycler can also be assigned to a variable and used outside or across125loops with the ``cycle()`` global function.126127128Mako129----130131You can configure Jinja to look more like Mako:132133.. code-block:: python134135    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    )145146With an environment configured like that, Jinja should be able to147interpret a small subset of Mako templates without any changes.148149Jinja does not support embedded Python code, so you would have to move150that out of the template. You could either process the data with the151same code before rendering, or add a global function or filter to the152Jinja environment.153154The syntax for defs (which are called macros in Jinja) and template155inheritance is different too.156157The following Mako template:158159.. code-block:: mako160161    <%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>168169Looks like this in Jinja with the above configuration:170171.. code-block:: jinja172173    <% extends "layout.html" %>174    <% block title %>Page Title<% endblock %>175    <% block body %>176    <ul>177    % for item in list:178        <li>${item}</li>179    % endfor180    </ul>181    <% endblock %>