Switching From Other Template Engines ===================================== This is a brief guide on some of the differences between Jinja syntax and other template languages. See :doc:`/templates` for a comprehensive guide to Jinja syntax and features. Django ------ If you have previously worked with Django templates, you should find Jinja very familiar. Many of the syntax elements look and work the same. However, Jinja provides some more syntax elements, and some work a bit differently. This section covers the template changes. The API, including extension support, is fundamentally different so it won't be covered here. Django supports using Jinja as its template engine, see https://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines. Method Calls ~~~~~~~~~~~~ In Django, methods are called implicitly, without parentheses. .. code-block:: django {% for page in user.get_created_pages %} ... {% endfor %} In Jinja, using parentheses is required for calls, like in Python. This allows you to pass variables to the method, which is not possible in Django. This syntax is also used for calling macros. .. code-block:: jinja {% for page in user.get_created_pages() %} ... {% endfor %} Filter Arguments ~~~~~~~~~~~~~~~~ In Django, one literal value can be passed to a filter after a colon. .. code-block:: django {{ items|join:", " }} In Jinja, filters can take any number of positional and keyword arguments in parentheses, like function calls. Arguments can also be variables instead of literal values. .. code-block:: jinja {{ items|join(", ") }} Tests ~~~~~ In addition to filters, Jinja also has "tests" used with the ``is`` operator. This operator is not the same as the Python operator. .. code-block:: jinja {% if user.user_id is odd %} {{ user.username|e }} is odd {% else %} hmm. {{ user.username|e }} looks pretty normal {% endif %} Loops ~~~~~ In Django, the special variable for the loop context is called ``forloop``, and the ``empty`` is used for no loop items. .. code-block:: django {% for item in items %} {{ item }} {% empty %} No items! {% endfor %} In Jinja, the special variable for the loop context is called ``loop``, and the ``else`` block is used for no loop items. .. code-block:: jinja {% for item in items %} {{ loop.index}}. {{ item }} {% else %} No items! {% endfor %} Cycle ~~~~~ In Django, the ``{% cycle %}`` can be used in a for loop to alternate between values per loop. .. code-block:: django {% for user in users %}