Skip to content
Myr

tricks.rst

This file

Path

docs/tricks.rst

100 lines · 3.1 KiB

Last changed by

Merge pull request #370 from BillyWM/patch-1Markus Unterwaditzerbefore adoptiond97db3f

History of this file

Take it

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

Some more small cleanup for metaclassesArmin Ronacherbefore adoption41746b9

lines 1–17why, one step back
1Tips and Tricks
2===============34.. highlight:: html+jinja56This part of the documentation shows some tips and tricks for Jinja27templates.8910.. _null-master-fallback:1112Null-Master Fallback13--------------------1415Jinja2 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` tag

Merge pull request #310 from vivekagr/patch-1Armin Ronacherbefore adoptionfff365e

line 18why, one step back
18including whitespace is printed out instead of being ignored, it can be used

Some more small cleanup for metaclassesArmin Ronacherbefore adoption41746b9

lines 19–21why, one step back
19for a neat trick.
2021Usually child templates extend from one template that adds a basic HTML

Merge pull request #310 from vivekagr/patch-1Armin Ronacherbefore adoptionfff365e

line 22why, one step back
22skeleton. However it's possible to put the `extends` tag into an `if` tag to

Some more small cleanup for metaclassesArmin Ronacherbefore adoption41746b9

lines 23–65why, one step back
23only extend from the layout template if the `standalone` variable evaluates
24to false which it does per default if it's not defined. Additionally a very25basic skeleton is added to the file so that if it's indeed rendered with26`standalone` set to `True` a very basic HTML skeleton is added::2728 {% if not standalone %}{% extends 'master.html' %}{% endif -%}29 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">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 amount 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 makes

Merge pull request #370 from BillyWM/patch-1Markus Unterwaditzerbefore adoptiond97db3f

line 66why, one step back
66sense to define a default for that variable::

Some more small cleanup for metaclassesArmin Ronacherbefore adoption41746b9

lines 67–95why, one step back
67
68 {% 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 %}77 <li{% if id == active_page %} class="active"{% endif78 %}><a href="{{ href|e }}">{{ caption|e }}</a></li>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 %}

Merge pull request #381 from ubershmekel/patch-1Markus Unterwaditzerbefore adoption92278b7

line 96why, one step back
96 <td id="cell-{{ rowloop.index }}-{{ loop.index }}">{{ cell }}</td>

Some more small cleanup for metaclassesArmin Ronacherbefore adoption41746b9

lines 97–100why, one step back
97 {% endfor %}
98 </tr>99 {% endfor %}100 </table>