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 fileTake 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 back1Switching From Other Template Enginesupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 2–3why, one step back2=====================================3Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 4–6why, one step back4This 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.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 7–12why, one step back789Django10------1112If you have previously worked with Django templates, you should findMerge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 13–15why, one step back13Jinja 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 back16Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 17–18why, one step back17This section covers the template changes. The API, including extension18support, is fundamentally different so it won't be covered here.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 19why, one step back19Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 20–21why, one step back20Django supports using Jinja as its template engine, see21https://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 back22Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 23why, one step back23update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 24–26why, one step back24Method Calls25~~~~~~~~~~~~26Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 27why, one step back27In Django, methods are called implicitly, without parentheses.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 28why, one step back28Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 29–30why, one step back29.. code-block:: django30update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 31–34why, one step back31 {% for page in user.get_created_pages %}32 ...33 {% endfor %}34Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 35–37why, one step back35In 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.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 38why, one step back38Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 39–40why, one step back39.. code-block:: jinja40update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 41–48why, one step back41 {% for page in user.get_created_pages() %}42 ...43 {% endfor %}444546Filter Arguments47~~~~~~~~~~~~~~~~48Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 49why, one step back49In 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 back50Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 51–52why, one step back51.. code-block:: django52update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 53–54why, one step back53 {{ items|join:", " }}54Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 55–57why, one step back55In Jinja, filters can take any number of positional and keyword56arguments 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 back58Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 59why, one step back59.. code-block:: jinjaupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 60why, one step back60Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 61why, one step back61 {{ items|join(", ") }}update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 62why, one step back62Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 63why, one step back63update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 64–66why, one step back64Tests65~~~~~66Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 67–68why, one step back67In 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 back69Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 70–71why, one step back70.. code-block:: jinja71update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 72–80why, one step back72 {% if user.user_id is odd %}73 {{ user.username|e }} is odd74 {% else %}75 hmm. {{ user.username|e }} looks pretty normal76 {% endif %}7778Loops79~~~~~80Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 81–82why, one step back81In Django, the special variable for the loop context is called82``forloop``, and the ``empty`` is used for no loop items.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 83why, one step back83Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 84why, one step back84.. code-block:: djangoupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 85–86why, one step back8586 {% for item in items %}Merge branch '3.1.x'David Lordbefore adoption5d9ece6
line 87why, one step back87 {{ forloop.counter }}. {{ item }}update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 88–91why, one step back88 {% empty %}89 No items!90 {% endfor %}91Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 92–93why, one step back92In 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 back94Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 95–96why, one step back95.. code-block:: jinja96update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 97why, one step back97 {% for item in items %}Merge branch '3.1.x'David Lordbefore adoption5d9ece6
line 98why, one step back98 {{ loop.index }}. {{ item }}update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 99–102why, one step back99 {% else %}100 No items!101 {% endfor %}102Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 103why, one step back103update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 104–106why, one step back104Cycle105~~~~~106Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 107–108why, one step back107In Django, the ``{% cycle %}`` can be used in a for loop to alternate108between values per loop.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 109why, one step back109Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 110why, one step back110.. code-block:: djangoupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 111–115why, one step back111112 {% for user in users %}113 <li class="{% cycle 'odd' 'even' %}">{{ user }}</li>114 {% endfor %}115Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 116why, one step back116In Jinja, the ``loop`` context has a ``cycle`` method.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 117why, one step back117Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 118–119why, one step back118.. code-block:: jinja119update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 120–123why, one step back120 {% for user in users %}121 <li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li>122 {% endfor %}123Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 124–125why, one step back124A cycler can also be assigned to a variable and used outside or across125loops with the ``cycle()`` global function.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 126–130why, one step back126127128Mako129----130Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 131why, one step back131You can configure Jinja to look more like Mako:update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 132why, one step back132Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
line 133why, one step back133.. code-block:: pythonupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 134why, one step back134Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 135–144why, one step back135 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 back145Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 146–147why, one step back146With an environment configured like that, Jinja should be able to147interpret a small subset of Mako templates without any changes.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 148why, one step back148Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 149–152why, one step back149Jinja 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.update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
line 153why, one step back153Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 154–160why, one step back154The syntax for defs (which are called macros in Jinja) and template155inheritance is different too.156157The following Mako template:158159.. code-block:: mako160update building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 161–168why, one step back161 <%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>168Merge pull request #1617 from pallets/docs-proseDavid Lordbefore adoption3a050b1
lines 169–171why, one step back169Looks like this in Jinja with the above configuration:170171.. code-block:: jinjaupdate building docs instructions in contributing guide (#1235)sixtyfathomsbefore adoptionc580dd0
lines 172–181why, one step back172173 <% 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 %>