| 1 |
Switching From Other Template Engines |
| 2 |
===================================== |
| 3 |
|
| 4 |
This is a brief guide on some of the differences between Jinja syntax |
| 5 |
and other template languages. See :doc:`/templates` for a comprehensive |
| 6 |
guide to Jinja syntax and features. |
| 7 |
|
| 8 |
|
| 9 |
Django |
| 10 |
------ |
| 11 |
|
| 12 |
If you have previously worked with Django templates, you should find |
| 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 bit |
| 15 |
differently. |
| 16 |
|
| 17 |
This section covers the template changes. The API, including extension |
| 18 |
support, is fundamentally different so it won't be covered here. |
| 19 |
|
| 20 |
Django supports using Jinja as its template engine, see |
| 21 |
https://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines. |
| 22 |
|
| 23 |
|
| 24 |
Method Calls |
| 25 |
~~~~~~~~~~~~ |
| 26 |
|
| 27 |
In Django, methods are called implicitly, without parentheses. |
| 28 |
|
| 29 |
.. code-block:: django |
| 30 |
|
| 31 |
{% for page in user.get_created_pages %} |
| 32 |
... |
| 33 |
{% endfor %} |
| 34 |
|
| 35 |
In Jinja, using parentheses is required for calls, like in Python. This |
| 36 |
allows you to pass variables to the method, which is not possible |
| 37 |
in Django. This syntax is also used for calling macros. |
| 38 |
|
| 39 |
.. code-block:: jinja |
| 40 |
|
| 41 |
{% for page in user.get_created_pages() %} |
| 42 |
... |
| 43 |
{% endfor %} |
| 44 |
|
| 45 |
|
| 46 |
Filter Arguments |
| 47 |
~~~~~~~~~~~~~~~~ |
| 48 |
|
| 49 |
In Django, one literal value can be passed to a filter after a colon. |
| 50 |
|
| 51 |
.. code-block:: django |
| 52 |
|
| 53 |
{{ items|join:", " }} |
| 54 |
|
| 55 |
In Jinja, filters can take any number of positional and keyword |
| 56 |
arguments in parentheses, like function calls. Arguments can also be |
| 57 |
variables instead of literal values. |
| 58 |
|
| 59 |
.. code-block:: jinja |
| 60 |
|
| 61 |
{{ items|join(", ") }} |
| 62 |
|
| 63 |
|
| 64 |
Tests |
| 65 |
~~~~~ |
| 66 |
|
| 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:: jinja |
| 71 |
|
| 72 |
{% if user.user_id is odd %} |
| 73 |
{{ user.username|e }} is odd |
| 74 |
{% else %} |
| 75 |
hmm. {{ user.username|e }} looks pretty normal |
| 76 |
{% endif %} |
| 77 |
|
| 78 |
Loops |
| 79 |
~~~~~ |
| 80 |
|
| 81 |
In Django, the special variable for the loop context is called |
| 82 |
``forloop``, and the ``empty`` is used for no loop items. |
| 83 |
|
| 84 |
.. code-block:: django |
| 85 |
|
| 86 |
{% for item in items %} |
| 87 |
{{ forloop.counter }}. {{ item }} |
| 88 |
{% empty %} |
| 89 |
No items! |
| 90 |
{% endfor %} |
| 91 |
|
| 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:: jinja |
| 96 |
|
| 97 |
{% for item in items %} |
| 98 |
{{ loop.index }}. {{ item }} |
| 99 |
{% else %} |
| 100 |
No items! |
| 101 |
{% endfor %} |
| 102 |
|
| 103 |
|
| 104 |
Cycle |
| 105 |
~~~~~ |
| 106 |
|
| 107 |
In Django, the ``{% cycle %}`` can be used in a for loop to alternate |
| 108 |
between values per loop. |
| 109 |
|
| 110 |
.. code-block:: django |
| 111 |
|
| 112 |
{% for user in users %} |
| 113 |
<li class="{% cycle 'odd' 'even' %}">{{ user }}</li> |
| 114 |
{% endfor %} |
| 115 |
|
| 116 |
In Jinja, the ``loop`` context has a ``cycle`` method. |
| 117 |
|
| 118 |
.. code-block:: jinja |
| 119 |
|
| 120 |
{% for user in users %} |
| 121 |
<li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li> |
| 122 |
{% endfor %} |
| 123 |
|
| 124 |
A cycler can also be assigned to a variable and used outside or across |
| 125 |
loops with the ``cycle()`` global function. |
| 126 |
|
| 127 |
|
| 128 |
Mako |
| 129 |
---- |
| 130 |
|
| 131 |
You can configure Jinja to look more like Mako: |
| 132 |
|
| 133 |
.. code-block:: python |
| 134 |
|
| 135 |
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 |
|
| 146 |
With an environment configured like that, Jinja should be able to |
| 147 |
interpret a small subset of Mako templates without any changes. |
| 148 |
|
| 149 |
Jinja does not support embedded Python code, so you would have to move |
| 150 |
that out of the template. You could either process the data with the |
| 151 |
same code before rendering, or add a global function or filter to the |
| 152 |
Jinja environment. |
| 153 |
|
| 154 |
The syntax for defs (which are called macros in Jinja) and template |
| 155 |
inheritance is different too. |
| 156 |
|
| 157 |
The following Mako template: |
| 158 |
|
| 159 |
.. code-block:: mako |
| 160 |
|
| 161 |
<%inherit file="layout.html" /> |
| 162 |
<%def name="title()">Page Title</%def> |
| 163 |
<ul> |
| 164 |
% for item in list: |
| 165 |
<li>${item}</li> |
| 166 |
% endfor |
| 167 |
</ul> |
| 168 |
|
| 169 |
Looks like this in Jinja with the above configuration: |
| 170 |
|
| 171 |
.. code-block:: jinja |
| 172 |
|
| 173 |
<% extends "layout.html" %> |
| 174 |
<% block title %>Page Title<% endblock %> |
| 175 |
<% block body %> |
| 176 |
<ul> |
| 177 |
% for item in list: |
| 178 |
<li>${item}</li> |
| 179 |
% endfor |
| 180 |
</ul> |
| 181 |
<% endblock %> |