Skip to content
Myr

Switching docs tweaks

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Adam Chainz · committed by Adam Chainz · parent c97fa10 · Browse files · 1 file+20 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+20 added−21 removed

  • other:+20 added−21 removed

1 file+20 added−21 removed

modified:docs/switching.rst+20 added-21 removed
98 unmodified lines
99100Method Calls101~~~~~~~~~~~~102103In Django method calls work implicitly. With Jinja2 you have to specify that104you want to call an object. Thus this Django code::103In Django method calls work implicitly; with Jinja2 you have to specify that104you want to call a method explicitly. Thus this Django code::105106 {% for page in user.get_created_pages %}107 ...108 {% endfor %}109 110will look like this in Jinja::109110...looks like this in Jinja::111112 {% for page in user.get_created_pages() %}113 ...114 {% endfor %}115116This allows you to pass variables to the function which is also used for macros117which is not possible in Django.116This allows you to pass variables to the method, which is not possible in117Django. This syntax is also used for macros.118119Conditions120~~~~~~~~~~121
36 unmodified lines
158looks like this in Jinja2::159160 {{ items|join(', ') }}161162In fact it's a bit more verbose but it allows different types of arguments -162It is a bit more verbose, but it allows different types of arguments -163including variables - and more than one of them.164165Tests166~~~~~
9 unmodified lines
176177Loops178~~~~~179180For loops work very similar to Django. Notably, in Jinja2 the special variable for 181the loop context is called `loop` and not `forloop` like in Django.180For loops work very similarly to Django, but notably the Jinja2 special181variable for the loop context is called `loop`, not `forloop` as in Django.182183In addition, the Django `empty` argument is called `else` in Jinja2. For example, the184Django template::183In addition, the Django `empty` argument is called `else` in Jinja2. For184example, the Django template::185186 {% for item in items %}187 {{ item }}188 {% empty %}189 No items!190 {% endfor %}191192would be handled in Jinja2 as::192...looks like this in Jinja2::193194 {% for item in items %}195 {{ item }}196 {% else %}
2 unmodified lines
199200Cycle201~~~~~202203The ``{% cycle %}`` tag does not exist in Jinja because of it's implicit204nature. However you can achieve mostly the same by using the `cycle`205method on a loop object.203The ``{% cycle %}`` tag does not exist in Jinja2; however, you can achieve the204same output by using the `cycle` method on the loop context special variable.205206The following Django template::207208 {% for user in users %}209 <li class="{% cycle 'odd' 'even' %}">{{ user }}</li>210 {% endfor %}211213Would look like this in Jinja::212...looks like this in Jinja2::213214 {% for user in users %}215 <li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li>216 {% endfor %}
12 unmodified lines
229.. sourcecode:: python230231 env = Environment('<%', '%>', '${', '}', '<%doc>', '</%doc>', '%', '##')232234Once the environment is configured like that Jinja2 should be able to interpret235a small subset of Mako templates. Jinja2 does not support embedded Python code236so you would have to move that out of the template. The syntax for defs (in237Jinja2 defs are called macros) and template inheritance is different too. The238following Mako template::233With an environment configured like that, Jinja2 should be able to interpret234a small subset of Mako templates. Jinja2 does not support embedded Python235code, so you would have to move that out of the template. The syntax for defs236(which are called macros in Jinja2) and template inheritance is different too.237The following Mako template::238239 <%inherit file="layout.html" />240 <%def name="title()">Page Title</%def>241 <ul>
16 unmodified lines