tricks.rst
This file
Path
docs/tricks.rst
100 lines · 3.1 KiB
Last changed by
improved thread safety of the LRUCache and fixed a bug in for loopsArmin Ronacherbefore adoptione224488
History of this fileTake it
some small documentation fixesArmin Ronacherbefore adoption2b22874
lines 1–2why, one step back1Tips and Tricks2===============added parsing code for "for item in seq recursive" and improved parser interface a bitArmin Ronacherbefore adoptionfdf9530
lines 3–5why, one step back34.. highlight:: html+jinja5some small documentation fixesArmin Ronacherbefore adoption2b22874
line 6why, one step back6This part of the documentation shows some tips and tricks for Jinja2added parsing code for "for item in seq recursive" and improved parser interface a bitArmin Ronacherbefore adoptionfdf9530
lines 7–67why, one step back7templates.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` tag18including whitespace is printed out instead of being igored, it can be used19for a neat trick.2021Usually child templates extend from one template that adds a basic HTML22skeleton. However it's possible put the `extends` tag into an `if` tag to23only extend from the layout template if the `standalone` variable evaluates24to 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 makes66sense to defined a default for that variable::67improved variable exporting, added TODO fileArmin Ronacherbefore adoption69e12db
line 68why, one step back68 {% set navigation_bar = [added parsing code for "for item in seq recursive" and improved parser interface a bitArmin Ronacherbefore adoptionfdf9530
lines 69–72why, one step back69 ('/', 'index', 'Index'),70 ('/downloads/', 'downloads', 'Downloads'),71 ('/about/', 'about', 'About')72 ] -%}improved variable exporting, added TODO fileArmin Ronacherbefore adoption69e12db
line 73why, one step back73 {% set active_page = active_page|default('index') -%}added parsing code for "for item in seq recursive" and improved parser interface a bitArmin Ronacherbefore adoptionfdf9530
lines 74–81why, one step back74 ...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 ...improved thread safety of the LRUCache and fixed a bug in for loopsArmin Ronacherbefore adoptione224488
lines 82–100why, one step back8283.. _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>