Skip to content
Myr

tricks.rst

This file

Path

docs/tricks.rst

100 lines · 3.1 KiB

Last changed by

Merge branch 'stable'David Lordbefore adoption6aeab5d

History of this file

Take it

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

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

lines 1–9why, one step back
1Tips and Tricks
2===============34.. highlight:: html+jinja56This part of the documentation shows some tips and tricks for Jinja7templates.89

Merge remote-tracking branch 'origin/3.0.x'David Lordbefore adoptionf1c51b3

line 10why, one step back
10.. _null-default-fallback:

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

line 11why, one step back
11

Merge remote-tracking branch 'origin/3.0.x'David Lordbefore adoptionf1c51b3

lines 12–13why, one step back
12Null-Default Fallback
13---------------------

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

lines 14–23why, one step back
14
15Jinja supports dynamic inheritance and does not distinguish between parent16and child template as long as no `extends` tag is visited. While this leads17to the surprising behavior that everything before the first `extends` tag18including whitespace is printed out instead of being ignored, it can be used19for a neat trick.2021Usually child templates extend from one template that adds a basic HTML22skeleton. However it's possible to put the `extends` tag into an `if` tag to23only extend from the layout template if the `standalone` variable evaluates

Merge branch 'stable'David Lordbefore adoption6aeab5d

line 24why, one step back
24to false, which it does by default if it's not defined. Additionally a very

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

lines 25–27why, one step back
25basic skeleton is added to the file so that if it's indeed rendered with
26`standalone` set to `True` a very basic HTML skeleton is added::27

Merge remote-tracking branch 'origin/3.0.x'David Lordbefore adoptionf1c51b3

line 28why, one step back
28 {% if not standalone %}{% extends 'default.html' %}{% endif -%}

Merge branch '3.0.x'David Lordbefore adoption80e6c4b

line 29why, one step back
29 <!DOCTYPE html>

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

lines 30–76why, one step back
30 <title>{% block title %}The Page Title{% endblock %}</title>
31 <link rel="stylesheet" href="style.css" type="text/css">32 {% block body %}33 <p>This is the page body.</p>34 {% endblock %}353637Alternating Rows38----------------3940If you want to have different styles for each row of a table or41list you can use the `cycle` method on the `loop` object::4243 <ul>44 {% for row in rows %}45 <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>46 {% endfor %}47 </ul>4849`cycle` can take an unlimited number of strings. Each time this50tag is encountered the next item from the list is rendered.515253Highlighting Active Menu Items54------------------------------5556Often you want to have a navigation bar with an active navigation57item. This is really simple to achieve. Because assignments outside58of `block`\s in child templates are global and executed before the layout59template is evaluated it's possible to define the active menu item in the60child template::6162 {% extends "layout.html" %}63 {% set active_page = "index" %}6465The layout template can then access `active_page`. Additionally it makes66sense to define a default for that variable::6768 {% set navigation_bar = [69 ('/', 'index', 'Index'),70 ('/downloads/', 'downloads', 'Downloads'),71 ('/about/', 'about', 'About')72 ] -%}73 {% set active_page = active_page|default('index') -%}74 ...75 <ul id="navigation">76 {% for href, id, caption in navigation_bar %}

Merge pull request #1620 from janfilips/patch-1David Lordbefore adoptiona292075

lines 77–78why, one step back
77 <li{% if id == active_page %} class="active"{% endif %}>
78 <a href="{{ href|e }}">{{ caption|e }}</a></li>

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

lines 79–100why, one step back
79 {% endfor %}
80 </ul>81 ...8283.. _accessing-the-parent-loop:8485Accessing the parent Loop86-------------------------8788The special `loop` variable always points to the innermost loop. If it's89desired to have access to an outer loop it's possible to alias it::9091 <table>92 {% for row in table %}93 <tr>94 {% set rowloop = loop %}95 {% for cell in row %}96 <td id="cell-{{ rowloop.index }}-{{ loop.index }}">{{ cell }}</td>97 {% endfor %}98 </tr>99 {% endfor %}100 </table>