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

1Tips and Tricks2===============34.. highlight:: html+jinja56This part of the documentation shows some tips and tricks for Jinja7templates.8910.. _null-default-fallback:1112Null-Default Fallback13---------------------1415Jinja 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 evaluates24to false, which it does by 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 'default.html' %}{% endif -%}29    <!DOCTYPE html>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 %}77      <li{% if id == active_page %} class="active"{% endif %}>78      <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 %}96        <td id="cell-{{ rowloop.index }}-{{ loop.index }}">{{ cell }}</td>97      {% endfor %}98      </tr>99    {% endfor %}100    </table>