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

The walk stopped after 200 commits: lines older than c580dd0 point at it.

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 1why, one step back
1Switching From Other Template Engines

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 2–3why, one step back
2=====================================
3

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 4–6why, one step back
4This is a brief guide on some of the differences between Jinja syntax
5and other template languages. See :doc:`/templates` for a comprehensive6guide to Jinja syntax and features.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 7–12why, one step back
7
89Django10------1112If you have previously worked with Django templates, you should find

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 13–15why, one step back
13Jinja very familiar. Many of the syntax elements look and work the same.
14However, Jinja provides some more syntax elements, and some work a bit15differently.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 16why, one step back
16

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 17–18why, one step back
17This section covers the template changes. The API, including extension
18support, is fundamentally different so it won't be covered here.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 19why, one step back
19

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 20–21why, one step back
20Django supports using Jinja as its template engine, see
21https://docs.djangoproject.com/en/stable/topics/templates/#support-for-template-engines.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 22why, one step back
22

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 23why, one step back
23

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 24–26why, one step back
24Method Calls
25~~~~~~~~~~~~26

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 27why, one step back
27In Django, methods are called implicitly, without parentheses.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 28why, one step back
28

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 29–30why, one step back
29.. code-block:: django
30

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 31–34why, one step back
31 {% for page in user.get_created_pages %}
32 ...33 {% endfor %}34

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 35–37why, one step back
35In Jinja, using parentheses is required for calls, like in Python. This
36allows you to pass variables to the method, which is not possible37in Django. This syntax is also used for calling macros.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 38why, one step back
38

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 39–40why, one step back
39.. code-block:: jinja
40

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 41–48why, one step back
41 {% for page in user.get_created_pages() %}
42 ...43 {% endfor %}444546Filter Arguments47~~~~~~~~~~~~~~~~48

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 49why, one step back
49In Django, one literal value can be passed to a filter after a colon.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 50why, one step back
50

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 51–52why, one step back
51.. code-block:: django
52

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 53–54why, one step back
53 {{ items|join:", " }}
54

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 55–57why, one step back
55In Jinja, filters can take any number of positional and keyword
56arguments in parentheses, like function calls. Arguments can also be57variables instead of literal values.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 58why, one step back
58

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 59why, one step back
59.. code-block:: jinja

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 60why, one step back
60

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 61why, one step back
61 {{ items|join(", ") }}

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 62why, one step back
62

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 63why, one step back
63

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 64–66why, one step back
64Tests
65~~~~~66

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 67–68why, one step back
67In addition to filters, Jinja also has "tests" used with the ``is``
68operator. This operator is not the same as the Python operator.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 69why, one step back
69

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 70–71why, one step back
70.. code-block:: jinja
71

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 72–80why, one step back
72 {% if user.user_id is odd %}
73 {{ user.username|e }} is odd74 {% else %}75 hmm. {{ user.username|e }} looks pretty normal76 {% endif %}7778Loops79~~~~~80

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 81–82why, one step back
81In Django, the special variable for the loop context is called
82``forloop``, and the ``empty`` is used for no loop items.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 83why, one step back
83

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 84why, one step back
84.. code-block:: django

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 85–86why, one step back
85
86 {% for item in items %}

Merge branch '3.1.x'David Lordbefore adoption5d9ece6

line 87why, one step back
87 {{ forloop.counter }}. {{ item }}

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 88–91why, one step back
88 {% empty %}
89 No items!90 {% endfor %}91

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 92–93why, one step back
92In Jinja, the special variable for the loop context is called ``loop``,
93and the ``else`` block is used for no loop items.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 94why, one step back
94

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 95–96why, one step back
95.. code-block:: jinja
96

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 97why, one step back
97 {% for item in items %}

Merge branch '3.1.x'David Lordbefore adoption5d9ece6

line 98why, one step back
98 {{ loop.index }}. {{ item }}

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 99–102why, one step back
99 {% else %}
100 No items!101 {% endfor %}102

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 103why, one step back
103

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 104–106why, one step back
104Cycle
105~~~~~106

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 107–108why, one step back
107In Django, the ``{% cycle %}`` can be used in a for loop to alternate
108between values per loop.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 109why, one step back
109

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 110why, one step back
110.. code-block:: django

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 111–115why, one step back
111
112 {% for user in users %}113 <li class="{% cycle 'odd' 'even' %}">{{ user }}</li>114 {% endfor %}115

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 116why, one step back
116In Jinja, the ``loop`` context has a ``cycle`` method.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 117why, one step back
117

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 118–119why, one step back
118.. code-block:: jinja
119

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 120–123why, one step back
120 {% for user in users %}
121 <li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li>122 {% endfor %}123

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 124–125why, one step back
124A cycler can also be assigned to a variable and used outside or across
125loops with the ``cycle()`` global function.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 126–130why, one step back
126
127128Mako129----130

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 131why, one step back
131You can configure Jinja to look more like Mako:

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 132why, one step back
132

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

line 133why, one step back
133.. code-block:: python

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 134why, one step back
134

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 135–144why, one step back
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 )

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 145why, one step back
145

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 146–147why, one step back
146With an environment configured like that, Jinja should be able to
147interpret a small subset of Mako templates without any changes.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 148why, one step back
148

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 149–152why, one step back
149Jinja does not support embedded Python code, so you would have to move
150that 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.

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

line 153why, one step back
153

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 154–160why, one step back
154The syntax for defs (which are called macros in Jinja) and template
155inheritance is different too.156157The following Mako template:158159.. code-block:: mako160

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 161–168why, one step back
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

Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1

lines 169–171why, one step back
169Looks like this in Jinja with the above configuration:
170171.. code-block:: jinja

update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0

lines 172–181why, one step back
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 % endfor180 </ul>181 <% endblock %>