Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 1 | Template Designer Documentation |
| 2 | =============================== |
| 3 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 4 | .. highlight:: html+jinja |
| 5 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 6 | This document describes the syntax and semantics of the template engine and |
| 7 | will be most useful as reference to those creating Jinja templates. As the |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 8 | template engine is very flexible, the configuration from the application can |
Markus Unterwaditzer | 459b908 | 2015-03-11 20:56:40 +0100 | [diff] [blame] | 9 | be slightly different from the code presented here in terms of delimiters and |
| 10 | behavior of undefined values. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 11 | |
| 12 | |
| 13 | Synopsis |
| 14 | -------- |
| 15 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 16 | A Jinja template is simply a text file. Jinja can generate any text-based |
Markus Unterwaditzer | 7ecfbe8 | 2015-03-11 20:58:42 +0100 | [diff] [blame] | 17 | format (HTML, XML, CSV, LaTeX, etc.). A Jinja template doesn't need to have a |
| 18 | specific extension: ``.html``, ``.xml``, or any other extension is just fine. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 19 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 20 | A template contains **variables** and/or **expressions**, which get replaced |
| 21 | with values when a template is *rendered*; and **tags**, which control the |
| 22 | logic of the template. The template syntax is heavily inspired by Django and |
| 23 | Python. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 24 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 25 | Below is a minimal template that illustrates a few basics using the default |
| 26 | Jinja configuration. We will cover the details later in this document:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 27 | |
Evan Sonderegger | b8230a7 | 2015-01-07 20:01:40 -0500 | [diff] [blame] | 28 | <!DOCTYPE html> |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 29 | <html lang="en"> |
| 30 | <head> |
| 31 | <title>My Webpage</title> |
| 32 | </head> |
| 33 | <body> |
| 34 | <ul id="navigation"> |
| 35 | {% for item in navigation %} |
| 36 | <li><a href="{{ item.href }}">{{ item.caption }}</a></li> |
| 37 | {% endfor %} |
| 38 | </ul> |
| 39 | |
| 40 | <h1>My Webpage</h1> |
| 41 | {{ a_variable }} |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 42 | |
| 43 | {# a comment #} |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 44 | </body> |
| 45 | </html> |
| 46 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 47 | The following example shows the default configuration settings. An application |
| 48 | developer can change the syntax configuration from ``{% foo %}`` to ``<% foo |
| 49 | %>``, or something similar. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 50 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 51 | There are a few kinds of delimiters. The default Jinja delimiters are |
| 52 | configured as follows: |
| 53 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 54 | * ``{% ... %}`` for :ref:`Statements <list-of-control-structures>` |
| 55 | * ``{{ ... }}`` for :ref:`Expressions` to print to the template output |
| 56 | * ``{# ... #}`` for :ref:`Comments` not included in the template output |
| 57 | * ``# ... ##`` for :ref:`Line Statements <line-statements>` |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 58 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 59 | |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 60 | .. _variables: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 61 | |
| 62 | Variables |
| 63 | --------- |
| 64 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 65 | Template variables are defined by the context dictionary passed to the |
| 66 | template. |
| 67 | |
Thomas Ballinger | f30ab12 | 2013-10-27 18:53:06 -0400 | [diff] [blame] | 68 | You can mess around with the variables in templates provided they are passed in |
| 69 | by the application. Variables may have attributes or elements on them you can |
| 70 | access too. What attributes a variable has depends heavily on the application |
| 71 | providing that variable. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 72 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 73 | You can use a dot (``.``) to access attributes of a variable in addition |
| 74 | to the standard Python ``__getitem__`` "subscript" syntax (``[]``). |
| 75 | |
| 76 | The following lines do the same thing:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 77 | |
| 78 | {{ foo.bar }} |
| 79 | {{ foo['bar'] }} |
| 80 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 81 | It's important to know that the outer double-curly braces are *not* part of the |
| 82 | variable, but the print statement. If you access variables inside tags don't |
| 83 | put the braces around them. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 84 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 85 | If a variable or attribute does not exist, you will get back an undefined |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 86 | value. What you can do with that kind of value depends on the application |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 87 | configuration: the default behavior is to evaluate to an empty string if |
| 88 | printed or iterated over, and to fail for every other operation. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 89 | |
Armin Ronacher | b938877 | 2008-06-25 20:43:18 +0200 | [diff] [blame] | 90 | .. _notes-on-subscriptions: |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 91 | |
| 92 | .. admonition:: Implementation |
| 93 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 94 | For the sake of convenience, ``foo.bar`` in Jinja2 does the following |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 95 | things on the Python layer: |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 96 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 97 | - check for an attribute called `bar` on `foo` |
| 98 | (``getattr(foo, 'bar')``) |
| 99 | - if there is not, check for an item ``'bar'`` in `foo` |
| 100 | (``foo.__getitem__('bar')``) |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 101 | - if there is not, return an undefined object. |
| 102 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 103 | ``foo['bar']`` works mostly the same with a small difference in sequence: |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 104 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 105 | - check for an item ``'bar'`` in `foo`. |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 106 | (``foo.__getitem__('bar')``) |
| 107 | - if there is not, check for an attribute called `bar` on `foo`. |
| 108 | (``getattr(foo, 'bar')``) |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 109 | - if there is not, return an undefined object. |
| 110 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 111 | This is important if an object has an item and attribute with the same |
| 112 | name. Additionally, the :func:`attr` filter only looks up attributes. |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 113 | |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 114 | .. _filters: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 115 | |
| 116 | Filters |
| 117 | ------- |
| 118 | |
Wieland Hoffmann | 0614747 | 2011-07-07 12:09:21 -0700 | [diff] [blame] | 119 | Variables can be modified by **filters**. Filters are separated from the |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 120 | variable by a pipe symbol (``|``) and may have optional arguments in |
| 121 | parentheses. Multiple filters can be chained. The output of one filter is |
| 122 | applied to the next. |
| 123 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 124 | For example, ``{{ name|striptags|title }}`` will remove all HTML Tags from |
| 125 | variable `name` and title-case the output (``title(striptags(name))``). |
| 126 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 127 | Filters that accept arguments have parentheses around the arguments, just like |
| 128 | a function call. For example: ``{{ listx|join(', ') }}`` will join a list with |
| 129 | commas (``str.join(', ', listx)``). |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 130 | |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 131 | The :ref:`builtin-filters` below describes all the builtin filters. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 132 | |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 133 | .. _tests: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 134 | |
| 135 | Tests |
| 136 | ----- |
| 137 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 138 | Beside filters, there are also so-called "tests" available. Tests can be used |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 139 | to test a variable against a common expression. To test a variable or |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 140 | expression, you add `is` plus the name of the test after the variable. For |
| 141 | example, to find out if a variable is defined, you can do ``name is defined``, |
| 142 | which will then return true or false depending on whether `name` is defined |
| 143 | in the current template context. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 144 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 145 | Tests can accept arguments, too. If the test only takes one argument, you can |
| 146 | leave out the parentheses. For example, the following two |
| 147 | expressions do the same thing:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 148 | |
| 149 | {% if loop.index is divisibleby 3 %} |
| 150 | {% if loop.index is divisibleby(3) %} |
| 151 | |
Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 152 | The :ref:`builtin-tests` below describes all the builtin tests. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 153 | |
| 154 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 155 | .. _comments: |
| 156 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 157 | Comments |
| 158 | -------- |
| 159 | |
| 160 | To comment-out part of a line in a template, use the comment syntax which is |
| 161 | by default set to ``{# ... #}``. This is useful to comment out parts of the |
| 162 | template for debugging or to add information for other template designers or |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 163 | yourself:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 164 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 165 | {# note: commented-out template because we no longer use this |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 166 | {% for user in users %} |
| 167 | ... |
| 168 | {% endfor %} |
| 169 | #} |
| 170 | |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 171 | |
| 172 | Whitespace Control |
| 173 | ------------------ |
| 174 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 175 | In the default configuration: |
| 176 | |
| 177 | * a single trailing newline is stripped if present |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 178 | * other whitespace (spaces, tabs, newlines etc.) is returned unchanged |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 179 | |
| 180 | If an application configures Jinja to `trim_blocks`, the first newline after a |
Kristi Tsukida | 214bb36 | 2012-07-10 18:13:52 -0700 | [diff] [blame] | 181 | template tag is removed automatically (like in PHP). The `lstrip_blocks` |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 182 | option can also be set to strip tabs and spaces from the beginning of a |
Kristi Tsukida | 214bb36 | 2012-07-10 18:13:52 -0700 | [diff] [blame] | 183 | line to the start of a block. (Nothing will be stripped if there are |
| 184 | other characters before the start of the block.) |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 185 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 186 | With both `trim_blocks` and `lstrip_blocks` enabled, you can put block tags |
Kristi Tsukida | 214bb36 | 2012-07-10 18:13:52 -0700 | [diff] [blame] | 187 | on their own lines, and the entire block line will be removed when |
| 188 | rendered, preserving the whitespace of the contents. For example, |
| 189 | without the `trim_blocks` and `lstrip_blocks` options, this template:: |
| 190 | |
| 191 | <div> |
| 192 | {% if True %} |
| 193 | yay |
| 194 | {% endif %} |
| 195 | </div> |
| 196 | |
| 197 | gets rendered with blank lines inside the div:: |
| 198 | |
| 199 | <div> |
| 200 | |
| 201 | yay |
| 202 | |
| 203 | </div> |
| 204 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 205 | But with both `trim_blocks` and `lstrip_blocks` enabled, the template block |
| 206 | lines are removed and other whitespace is preserved:: |
Kristi Tsukida | 214bb36 | 2012-07-10 18:13:52 -0700 | [diff] [blame] | 207 | |
| 208 | <div> |
| 209 | yay |
| 210 | </div> |
| 211 | |
Kristi Tsukida | e4a74f9 | 2012-07-12 13:44:54 -0700 | [diff] [blame] | 212 | You can manually disable the `lstrip_blocks` behavior by putting a |
| 213 | plus sign (``+``) at the start of a block:: |
| 214 | |
| 215 | <div> |
| 216 | {%+ if something %}yay{% endif %} |
| 217 | </div> |
| 218 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 219 | You can also strip whitespace in templates by hand. If you add a minus |
| 220 | sign (``-``) to the start or end of a block (e.g. a :ref:`for-loop` tag), a |
| 221 | comment, or a variable expression, the whitespaces before or after |
| 222 | that block will be removed:: |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 223 | |
| 224 | {% for item in seq -%} |
| 225 | {{ item }} |
| 226 | {%- endfor %} |
Andy McKay | 2004449 | 2011-06-30 14:40:05 -0700 | [diff] [blame] | 227 | |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 228 | This will yield all elements without whitespace between them. If `seq` was |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 229 | a list of numbers from ``1`` to ``9``, the output would be ``123456789``. |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 230 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 231 | If :ref:`line-statements` are enabled, they strip leading whitespace |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 232 | automatically up to the beginning of the line. |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 233 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 234 | By default, Jinja2 also removes trailing newlines. To keep single |
| 235 | trailing newlines, configure Jinja to `keep_trailing_newline`. |
Armin Ronacher | a65f1eb | 2013-05-19 11:18:19 +0100 | [diff] [blame] | 236 | |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 237 | .. admonition:: Note |
| 238 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 239 | You must not add whitespace between the tag and the minus sign. |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 240 | |
| 241 | **valid**:: |
Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 242 | |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 243 | {%- if foo -%}...{% endif %} |
| 244 | |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 245 | **invalid**:: |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 246 | |
| 247 | {% - if foo - %}...{% endif %} |
| 248 | |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 249 | |
| 250 | Escaping |
| 251 | -------- |
| 252 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 253 | It is sometimes desirable -- even necessary -- to have Jinja ignore parts |
| 254 | it would otherwise handle as variables or blocks. For example, if, with |
| 255 | the default syntax, you want to use ``{{`` as a raw string in a template and |
| 256 | not start a variable, you have to use a trick. |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 257 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 258 | The easiest way to output a literal variable delimiter (``{{``) is by using a |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 259 | variable expression:: |
| 260 | |
| 261 | {{ '{{' }} |
| 262 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 263 | For bigger sections, it makes sense to mark a block `raw`. For example, to |
| 264 | include example Jinja syntax in a template, you can use this snippet:: |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 265 | |
| 266 | {% raw %} |
| 267 | <ul> |
| 268 | {% for item in seq %} |
| 269 | <li>{{ item }}</li> |
| 270 | {% endfor %} |
| 271 | </ul> |
| 272 | {% endraw %} |
| 273 | |
| 274 | |
| 275 | .. _line-statements: |
| 276 | |
| 277 | Line Statements |
| 278 | --------------- |
| 279 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 280 | If line statements are enabled by the application, it's possible to mark a |
| 281 | line as a statement. For example, if the line statement prefix is configured |
| 282 | to ``#``, the following two examples are equivalent:: |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 283 | |
| 284 | <ul> |
| 285 | # for item in seq |
| 286 | <li>{{ item }}</li> |
| 287 | # endfor |
| 288 | </ul> |
| 289 | |
| 290 | <ul> |
| 291 | {% for item in seq %} |
| 292 | <li>{{ item }}</li> |
| 293 | {% endfor %} |
| 294 | </ul> |
| 295 | |
| 296 | The line statement prefix can appear anywhere on the line as long as no text |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 297 | precedes it. For better readability, statements that start a block (such as |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 298 | `for`, `if`, `elif` etc.) may end with a colon:: |
| 299 | |
| 300 | # for item in seq: |
| 301 | ... |
Georg Brandl | ac61b24 | 2008-05-11 12:30:19 +0200 | [diff] [blame] | 302 | # endfor |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame] | 303 | |
| 304 | |
Armin Ronacher | 3ef2043 | 2008-06-09 18:27:19 +0200 | [diff] [blame] | 305 | .. admonition:: Note |
| 306 | |
| 307 | Line statements can span multiple lines if there are open parentheses, |
| 308 | braces or brackets:: |
| 309 | |
| 310 | <ul> |
| 311 | # for href, caption in [('index.html', 'Index'), |
| 312 | ('about.html', 'About')]: |
| 313 | <li><a href="{{ href }}">{{ caption }}</a></li> |
| 314 | # endfor |
| 315 | </ul> |
| 316 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 317 | Since Jinja 2.2, line-based comments are available as well. For example, if |
| 318 | the line-comment prefix is configured to be ``##``, everything from ``##`` to |
Armin Ronacher | 59b6bd5 | 2009-03-30 21:00:16 +0200 | [diff] [blame] | 319 | the end of the line is ignored (excluding the newline sign):: |
| 320 | |
| 321 | # for item in seq: |
| 322 | <li>{{ item }}</li> ## this comment is ignored |
| 323 | # endfor |
| 324 | |
Armin Ronacher | 3ef2043 | 2008-06-09 18:27:19 +0200 | [diff] [blame] | 325 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 326 | .. _template-inheritance: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 327 | |
| 328 | Template Inheritance |
| 329 | -------------------- |
| 330 | |
| 331 | The most powerful part of Jinja is template inheritance. Template inheritance |
| 332 | allows you to build a base "skeleton" template that contains all the common |
| 333 | elements of your site and defines **blocks** that child templates can override. |
| 334 | |
| 335 | Sounds complicated but is very basic. It's easiest to understand it by starting |
| 336 | with an example. |
| 337 | |
| 338 | |
| 339 | Base Template |
| 340 | ~~~~~~~~~~~~~ |
| 341 | |
| 342 | This template, which we'll call ``base.html``, defines a simple HTML skeleton |
| 343 | document that you might use for a simple two-column page. It's the job of |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 344 | "child" templates to fill the empty blocks with content:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 345 | |
Evan Sonderegger | b8230a7 | 2015-01-07 20:01:40 -0500 | [diff] [blame] | 346 | <!DOCTYPE html> |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 347 | <html lang="en"> |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 348 | <head> |
| 349 | {% block head %} |
| 350 | <link rel="stylesheet" href="style.css" /> |
| 351 | <title>{% block title %}{% endblock %} - My Webpage</title> |
| 352 | {% endblock %} |
| 353 | </head> |
| 354 | <body> |
| 355 | <div id="content">{% block content %}{% endblock %}</div> |
| 356 | <div id="footer"> |
| 357 | {% block footer %} |
| 358 | © Copyright 2008 by <a href="http://domain.invalid/">you</a>. |
| 359 | {% endblock %} |
| 360 | </div> |
| 361 | </body> |
Evan Sonderegger | b8230a7 | 2015-01-07 20:01:40 -0500 | [diff] [blame] | 362 | </html> |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 363 | |
| 364 | In this example, the ``{% block %}`` tags define four blocks that child templates |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 365 | can fill in. All the `block` tag does is tell the template engine that a |
| 366 | child template may override those placeholders in the template. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 367 | |
| 368 | Child Template |
| 369 | ~~~~~~~~~~~~~~ |
| 370 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 371 | A child template might look like this:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 372 | |
| 373 | {% extends "base.html" %} |
| 374 | {% block title %}Index{% endblock %} |
| 375 | {% block head %} |
| 376 | {{ super() }} |
| 377 | <style type="text/css"> |
| 378 | .important { color: #336699; } |
| 379 | </style> |
| 380 | {% endblock %} |
| 381 | {% block content %} |
| 382 | <h1>Index</h1> |
| 383 | <p class="important"> |
Carlos Eduardo Rivera | cf845c9 | 2015-01-18 20:22:34 -0600 | [diff] [blame] | 384 | Welcome to my awesome homepage. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 385 | </p> |
| 386 | {% endblock %} |
| 387 | |
| 388 | The ``{% extends %}`` tag is the key here. It tells the template engine that |
| 389 | this template "extends" another template. When the template system evaluates |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 390 | this template, it first locates the parent. The extends tag should be the |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 391 | first tag in the template. Everything before it is printed out normally and |
Armin Ronacher | fdf9530 | 2008-05-11 22:20:51 +0200 | [diff] [blame] | 392 | may cause confusion. For details about this behavior and how to take |
| 393 | advantage of it, see :ref:`null-master-fallback`. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 394 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 395 | The filename of the template depends on the template loader. For example, the |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 396 | :class:`FileSystemLoader` allows you to access other templates by giving the |
Christoph Zwerschke | a21f2ed | 2011-10-23 18:23:58 +0200 | [diff] [blame] | 397 | filename. You can access templates in subdirectories with a slash:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 398 | |
| 399 | {% extends "layout/default.html" %} |
| 400 | |
| 401 | But this behavior can depend on the application embedding Jinja. Note that |
| 402 | since the child template doesn't define the ``footer`` block, the value from |
| 403 | the parent template is used instead. |
| 404 | |
| 405 | You can't define multiple ``{% block %}`` tags with the same name in the |
| 406 | same template. This limitation exists because a block tag works in "both" |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 407 | directions. That is, a block tag doesn't just provide a placeholder to fill |
| 408 | - it also defines the content that fills the placeholder in the *parent*. |
| 409 | If there were two similarly-named ``{% block %}`` tags in a template, |
| 410 | that template's parent wouldn't know which one of the blocks' content to use. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 411 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 412 | If you want to print a block multiple times, you can, however, use the special |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 413 | `self` variable and call the block with that name:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 414 | |
| 415 | <title>{% block title %}{% endblock %}</title> |
| 416 | <h1>{{ self.title() }}</h1> |
| 417 | {% block body %}{% endblock %} |
| 418 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 419 | |
| 420 | Super Blocks |
| 421 | ~~~~~~~~~~~~ |
| 422 | |
| 423 | It's possible to render the contents of the parent block by calling `super`. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 424 | This gives back the results of the parent block:: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 425 | |
| 426 | {% block sidebar %} |
| 427 | <h3>Table Of Contents</h3> |
| 428 | ... |
| 429 | {{ super() }} |
| 430 | {% endblock %} |
| 431 | |
| 432 | |
Armin Ronacher | fd31049 | 2008-05-25 00:16:51 +0200 | [diff] [blame] | 433 | Named Block End-Tags |
| 434 | ~~~~~~~~~~~~~~~~~~~~ |
| 435 | |
| 436 | Jinja2 allows you to put the name of the block after the end tag for better |
| 437 | readability:: |
| 438 | |
| 439 | {% block sidebar %} |
| 440 | {% block inner_sidebar %} |
| 441 | ... |
| 442 | {% endblock inner_sidebar %} |
| 443 | {% endblock sidebar %} |
| 444 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 445 | However, the name after the `endblock` word must match the block name. |
Armin Ronacher | fd31049 | 2008-05-25 00:16:51 +0200 | [diff] [blame] | 446 | |
| 447 | |
Armin Ronacher | 74a0cd9 | 2009-02-19 15:56:53 +0100 | [diff] [blame] | 448 | Block Nesting and Scope |
| 449 | ~~~~~~~~~~~~~~~~~~~~~~~ |
| 450 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 451 | Blocks can be nested for more complex layouts. However, per default blocks |
Armin Ronacher | 74a0cd9 | 2009-02-19 15:56:53 +0100 | [diff] [blame] | 452 | may not access variables from outer scopes:: |
| 453 | |
| 454 | {% for item in seq %} |
| 455 | <li>{% block loop_item %}{{ item }}{% endblock %}</li> |
| 456 | {% endfor %} |
| 457 | |
| 458 | This example would output empty ``<li>`` items because `item` is unavailable |
| 459 | inside the block. The reason for this is that if the block is replaced by |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 460 | a child template, a variable would appear that was not defined in the block or |
Armin Ronacher | 74a0cd9 | 2009-02-19 15:56:53 +0100 | [diff] [blame] | 461 | passed to the context. |
| 462 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 463 | Starting with Jinja 2.2, you can explicitly specify that variables are |
Armin Ronacher | 74a0cd9 | 2009-02-19 15:56:53 +0100 | [diff] [blame] | 464 | available in a block by setting the block to "scoped" by adding the `scoped` |
| 465 | modifier to a block declaration:: |
| 466 | |
| 467 | {% for item in seq %} |
| 468 | <li>{% block loop_item scoped %}{{ item }}{% endblock %}</li> |
| 469 | {% endfor %} |
| 470 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 471 | When overriding a block, the `scoped` modifier does not have to be provided. |
Armin Ronacher | 74a0cd9 | 2009-02-19 15:56:53 +0100 | [diff] [blame] | 472 | |
| 473 | |
Armin Ronacher | 9165d3e | 2010-02-16 17:35:59 +0100 | [diff] [blame] | 474 | Template Objects |
| 475 | ~~~~~~~~~~~~~~~~ |
| 476 | |
| 477 | .. versionchanged:: 2.4 |
| 478 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 479 | If a template object was passed in the template context, you can |
Armin Ronacher | 9165d3e | 2010-02-16 17:35:59 +0100 | [diff] [blame] | 480 | extend from that object as well. Assuming the calling code passes |
| 481 | a layout template as `layout_template` to the environment, this |
| 482 | code works:: |
| 483 | |
| 484 | {% extends layout_template %} |
| 485 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 486 | Previously, the `layout_template` variable had to be a string with |
Armin Ronacher | 9165d3e | 2010-02-16 17:35:59 +0100 | [diff] [blame] | 487 | the layout template's filename for this to work. |
| 488 | |
| 489 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 490 | HTML Escaping |
| 491 | ------------- |
| 492 | |
| 493 | When generating HTML from templates, there's always a risk that a variable will |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 494 | include characters that affect the resulting HTML. There are two approaches: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 495 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 496 | a. manually escaping each variable; or |
| 497 | b. automatically escaping everything by default. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 498 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 499 | Jinja supports both. What is used depends on the application configuration. |
| 500 | The default configuration is no automatic escaping; for various reasons: |
| 501 | |
| 502 | - Escaping everything except for safe values will also mean that Jinja is |
| 503 | escaping variables known to not include HTML (e.g. numbers, booleans) |
| 504 | which can be a huge performance hit. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 505 | |
| 506 | - The information about the safety of a variable is very fragile. It could |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 507 | happen that by coercing safe and unsafe values, the return value is |
| 508 | double-escaped HTML. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 509 | |
| 510 | Working with Manual Escaping |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 511 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 512 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 513 | If manual escaping is enabled, it's **your** responsibility to escape |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 514 | variables if needed. What to escape? If you have a variable that *may* |
| 515 | include any of the following chars (``>``, ``<``, ``&``, or ``"``) you |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 516 | **SHOULD** escape it unless the variable contains well-formed and trusted |
| 517 | HTML. Escaping works by piping the variable through the ``|e`` filter:: |
| 518 | |
| 519 | {{ user.username|e }} |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 520 | |
| 521 | Working with Automatic Escaping |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 522 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 523 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 524 | When automatic escaping is enabled, everything is escaped by default except |
| 525 | for values explicitly marked as safe. Variables and expressions |
| 526 | can be marked as safe either in: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 527 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 528 | a. the context dictionary by the application with `MarkupSafe.Markup`, or |
| 529 | b. the template, with the `|safe` filter |
| 530 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 531 | The main problem with this approach is that Python itself doesn't have the |
| 532 | concept of tainted values; so whether a value is safe or unsafe can get lost. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 533 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 534 | If a value is not marked safe, auto-escaping will take place; which means that |
| 535 | you could end up with double-escaped contents. Double-escaping is easy to |
| 536 | avoid, however: just rely on the tools Jinja2 provides and *don't use builtin |
| 537 | Python constructs such as str.format or the string modulo operator (%)*. |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 538 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 539 | Jinja2 functions (macros, `super`, `self.BLOCKNAME`) always return template |
| 540 | data that is marked as safe. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 541 | |
| 542 | String literals in templates with automatic escaping are considered unsafe |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 543 | because native Python strings (``str``, ``unicode``, ``basestring``) are not |
| 544 | `MarkupSafe.Markup` strings with an ``__html__`` attribute. |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 545 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 546 | .. _list-of-control-structures: |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 547 | |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 548 | List of Control Structures |
| 549 | -------------------------- |
| 550 | |
| 551 | A control structure refers to all those things that control the flow of a |
| 552 | program - conditionals (i.e. if/elif/else), for-loops, as well as things like |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 553 | macros and blocks. With the default syntax, control structures appear inside |
| 554 | ``{% ... %}`` blocks. |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 555 | |
| 556 | .. _for-loop: |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 557 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 558 | For |
| 559 | ~~~ |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 560 | |
Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 561 | Loop over each item in a sequence. For example, to display a list of users |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 562 | provided in a variable called `users`:: |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 563 | |
| 564 | <h1>Members</h1> |
| 565 | <ul> |
| 566 | {% for user in users %} |
| 567 | <li>{{ user.username|e }}</li> |
| 568 | {% endfor %} |
| 569 | </ul> |
| 570 | |
Armin Ronacher | 898975d | 2011-10-06 10:09:31 -0400 | [diff] [blame] | 571 | As variables in templates retain their object properties, it is possible to |
| 572 | iterate over containers like `dict`:: |
| 573 | |
| 574 | <dl> |
| 575 | {% for key, value in my_dict.iteritems() %} |
| 576 | <dt>{{ key|e }}</dt> |
| 577 | <dd>{{ value|e }}</dd> |
| 578 | {% endfor %} |
| 579 | </dl> |
| 580 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 581 | Note, however, that **Python dicts are not ordered**; so you might want to |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 582 | either pass a sorted ``list`` of ``tuple`` s -- or a |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 583 | ``collections.OrderedDict`` -- to the template, or use the `dictsort` filter. |
Armin Ronacher | 898975d | 2011-10-06 10:09:31 -0400 | [diff] [blame] | 584 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 585 | Inside of a for-loop block, you can access some special variables: |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 586 | |
| 587 | +-----------------------+---------------------------------------------------+ |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 588 | | Variable | Description | |
| 589 | +=======================+===================================================+ |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 590 | | `loop.index` | The current iteration of the loop. (1 indexed) | |
| 591 | +-----------------------+---------------------------------------------------+ |
| 592 | | `loop.index0` | The current iteration of the loop. (0 indexed) | |
| 593 | +-----------------------+---------------------------------------------------+ |
| 594 | | `loop.revindex` | The number of iterations from the end of the loop | |
| 595 | | | (1 indexed) | |
| 596 | +-----------------------+---------------------------------------------------+ |
| 597 | | `loop.revindex0` | The number of iterations from the end of the loop | |
| 598 | | | (0 indexed) | |
| 599 | +-----------------------+---------------------------------------------------+ |
| 600 | | `loop.first` | True if first iteration. | |
| 601 | +-----------------------+---------------------------------------------------+ |
| 602 | | `loop.last` | True if last iteration. | |
| 603 | +-----------------------+---------------------------------------------------+ |
| 604 | | `loop.length` | The number of items in the sequence. | |
| 605 | +-----------------------+---------------------------------------------------+ |
| 606 | | `loop.cycle` | A helper function to cycle between a list of | |
| 607 | | | sequences. See the explanation below. | |
| 608 | +-----------------------+---------------------------------------------------+ |
Armin Ronacher | 568352e | 2013-05-20 09:26:57 +0100 | [diff] [blame] | 609 | | `loop.depth` | Indicates how deep in deep in a recursive loop | |
| 610 | | | the rendering currently is. Starts at level 1 | |
| 611 | +-----------------------+---------------------------------------------------+ |
Armin Ronacher | da94a8b | 2013-05-20 14:06:59 +0100 | [diff] [blame] | 612 | | `loop.depth0` | Indicates how deep in deep in a recursive loop | |
Armin Ronacher | 568352e | 2013-05-20 09:26:57 +0100 | [diff] [blame] | 613 | | | the rendering currently is. Starts at level 0 | |
| 614 | +-----------------------+---------------------------------------------------+ |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 615 | |
Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 616 | Within a for-loop, it's possible to cycle among a list of strings/variables |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 617 | each time through the loop by using the special `loop.cycle` helper:: |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 618 | |
| 619 | {% for row in rows %} |
| 620 | <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li> |
| 621 | {% endfor %} |
| 622 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 623 | Since Jinja 2.1, an extra `cycle` helper exists that allows loop-unbound |
| 624 | cycling. For more information, have a look at the :ref:`builtin-globals`. |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 625 | |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 626 | .. _loop-filtering: |
| 627 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 628 | Unlike in Python, it's not possible to `break` or `continue` in a loop. You |
| 629 | can, however, filter the sequence during iteration, which allows you to skip |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 630 | items. The following example skips all the users which are hidden:: |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 631 | |
| 632 | {% for user in users if not user.hidden %} |
| 633 | <li>{{ user.username|e }}</li> |
| 634 | {% endfor %} |
| 635 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 636 | The advantage is that the special `loop` variable will count correctly; thus |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 637 | not counting the users not iterated over. |
| 638 | |
| 639 | If no iteration took place because the sequence was empty or the filtering |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 640 | removed all the items from the sequence, you can render a default block |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 641 | by using `else`:: |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 642 | |
| 643 | <ul> |
| 644 | {% for user in users %} |
| 645 | <li>{{ user.username|e }}</li> |
| 646 | {% else %} |
| 647 | <li><em>no users found</em></li> |
Armin Ronacher | 890dac0 | 2010-02-06 16:36:52 +0100 | [diff] [blame] | 648 | {% endfor %} |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 649 | </ul> |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 650 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 651 | Note that, in Python, `else` blocks are executed whenever the corresponding |
| 652 | loop **did not** `break`. Since Jinja loops cannot `break` anyway, |
Christoph Zwerschke | 4780c2c | 2012-03-21 16:56:36 +0100 | [diff] [blame] | 653 | a slightly different behavior of the `else` keyword was chosen. |
| 654 | |
Armin Ronacher | 1de4c64 | 2008-05-11 23:55:02 +0200 | [diff] [blame] | 655 | It is also possible to use loops recursively. This is useful if you are |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 656 | dealing with recursive data such as sitemaps or RDFa. |
| 657 | To use loops recursively, you basically have to add the `recursive` modifier |
| 658 | to the loop definition and call the `loop` variable with the new iterable |
| 659 | where you want to recurse. |
Armin Ronacher | 1de4c64 | 2008-05-11 23:55:02 +0200 | [diff] [blame] | 660 | |
| 661 | The following example implements a sitemap with recursive loops:: |
| 662 | |
| 663 | <ul class="sitemap"> |
| 664 | {%- for item in sitemap recursive %} |
| 665 | <li><a href="{{ item.href|e }}">{{ item.title }}</a> |
| 666 | {%- if item.children -%} |
| 667 | <ul class="submenu">{{ loop(item.children) }}</ul> |
| 668 | {%- endif %}</li> |
| 669 | {%- endfor %} |
| 670 | </ul> |
| 671 | |
Dimitris Leventeas | aa023e4 | 2012-11-01 23:21:32 +0100 | [diff] [blame] | 672 | The `loop` variable always refers to the closest (innermost) loop. If we |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 673 | have more than one level of loops, we can rebind the variable `loop` by |
Dimitris Leventeas | aa023e4 | 2012-11-01 23:21:32 +0100 | [diff] [blame] | 674 | writing `{% set outer_loop = loop %}` after the loop that we want to |
| 675 | use recursively. Then, we can call it using `{{ outer_loop(...) }}` |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 676 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 677 | .. _if: |
| 678 | |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 679 | If |
| 680 | ~~ |
| 681 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 682 | The `if` statement in Jinja is comparable with the Python if statement. |
| 683 | In the simplest form, you can use it to test if a variable is defined, not |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 684 | empty or not false:: |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 685 | |
| 686 | {% if users %} |
| 687 | <ul> |
| 688 | {% for user in users %} |
| 689 | <li>{{ user.username|e }}</li> |
| 690 | {% endfor %} |
| 691 | </ul> |
| 692 | {% endif %} |
| 693 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 694 | For multiple branches, `elif` and `else` can be used like in Python. You can |
| 695 | use more complex :ref:`expressions` there, too:: |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 696 | |
| 697 | {% if kenny.sick %} |
| 698 | Kenny is sick. |
| 699 | {% elif kenny.dead %} |
| 700 | You killed Kenny! You bastard!!! |
| 701 | {% else %} |
| 702 | Kenny looks okay --- so far |
| 703 | {% endif %} |
| 704 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 705 | If can also be used as an :ref:`inline expression <if-expression>` and for |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 706 | :ref:`loop filtering <loop-filtering>`. |
| 707 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 708 | .. _macros: |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 709 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 710 | Macros |
| 711 | ~~~~~~ |
| 712 | |
Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 713 | Macros are comparable with functions in regular programming languages. They |
| 714 | are useful to put often used idioms into reusable functions to not repeat |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 715 | yourself ("DRY"). |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 716 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 717 | Here's a small example of a macro that renders a form element:: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 718 | |
| 719 | {% macro input(name, value='', type='text', size=20) -%} |
| 720 | <input type="{{ type }}" name="{{ name }}" value="{{ |
| 721 | value|e }}" size="{{ size }}"> |
| 722 | {%- endmacro %} |
| 723 | |
| 724 | The macro can then be called like a function in the namespace:: |
| 725 | |
| 726 | <p>{{ input('username') }}</p> |
| 727 | <p>{{ input('password', type='password') }}</p> |
| 728 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 729 | If the macro was defined in a different template, you have to |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 730 | :ref:`import <import>` it first. |
| 731 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 732 | Inside macros, you have access to three special variables: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 733 | |
| 734 | `varargs` |
Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 735 | If more positional arguments are passed to the macro than accepted by the |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 736 | macro, they end up in the special `varargs` variable as a list of values. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 737 | |
| 738 | `kwargs` |
| 739 | Like `varargs` but for keyword arguments. All unconsumed keyword |
| 740 | arguments are stored in this special variable. |
| 741 | |
| 742 | `caller` |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 743 | If the macro was called from a :ref:`call<call>` tag, the caller is stored |
| 744 | in this variable as a callable macro. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 745 | |
| 746 | Macros also expose some of their internal details. The following attributes |
| 747 | are available on a macro object: |
| 748 | |
| 749 | `name` |
| 750 | The name of the macro. ``{{ input.name }}`` will print ``input``. |
| 751 | |
| 752 | `arguments` |
| 753 | A tuple of the names of arguments the macro accepts. |
| 754 | |
| 755 | `defaults` |
| 756 | A tuple of default values. |
| 757 | |
| 758 | `catch_kwargs` |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 759 | This is `true` if the macro accepts extra keyword arguments (i.e.: accesses |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 760 | the special `kwargs` variable). |
| 761 | |
| 762 | `catch_varargs` |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 763 | This is `true` if the macro accepts extra positional arguments (i.e.: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 764 | accesses the special `varargs` variable). |
| 765 | |
| 766 | `caller` |
| 767 | This is `true` if the macro accesses the special `caller` variable and may |
Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 768 | be called from a :ref:`call<call>` tag. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 769 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 770 | If a macro name starts with an underscore, it's not exported and can't |
Armin Ronacher | c347ed0 | 2008-09-20 12:04:53 +0200 | [diff] [blame] | 771 | be imported. |
| 772 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 773 | |
| 774 | .. _call: |
| 775 | |
| 776 | Call |
| 777 | ~~~~ |
| 778 | |
| 779 | In some cases it can be useful to pass a macro to another macro. For this |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 780 | purpose, you can use the special `call` block. The following example shows |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 781 | a macro that takes advantage of the call functionality and how it can be |
| 782 | used:: |
| 783 | |
| 784 | {% macro render_dialog(title, class='dialog') -%} |
| 785 | <div class="{{ class }}"> |
| 786 | <h2>{{ title }}</h2> |
| 787 | <div class="contents"> |
| 788 | {{ caller() }} |
| 789 | </div> |
| 790 | </div> |
| 791 | {%- endmacro %} |
| 792 | |
| 793 | {% call render_dialog('Hello World') %} |
| 794 | This is a simple dialog rendered by using a macro and |
| 795 | a call block. |
| 796 | {% endcall %} |
| 797 | |
| 798 | It's also possible to pass arguments back to the call block. This makes it |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 799 | useful as a replacement for loops. Generally speaking, a call block works |
| 800 | exactly like a macro without a name. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 801 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 802 | Here's an example of how a call block can be used with arguments:: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 803 | |
| 804 | {% macro dump_users(users) -%} |
| 805 | <ul> |
| 806 | {%- for user in users %} |
| 807 | <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li> |
| 808 | {%- endfor %} |
| 809 | </ul> |
| 810 | {%- endmacro %} |
| 811 | |
| 812 | {% call(user) dump_users(list_of_user) %} |
| 813 | <dl> |
| 814 | <dl>Realname</dl> |
| 815 | <dd>{{ user.realname|e }}</dd> |
| 816 | <dl>Description</dl> |
| 817 | <dd>{{ user.description }}</dd> |
| 818 | </dl> |
| 819 | {% endcall %} |
| 820 | |
| 821 | |
Armin Ronacher | 76f9aa4 | 2008-07-12 02:08:29 +0200 | [diff] [blame] | 822 | Filters |
| 823 | ~~~~~~~ |
| 824 | |
| 825 | Filter sections allow you to apply regular Jinja2 filters on a block of |
| 826 | template data. Just wrap the code in the special `filter` section:: |
| 827 | |
| 828 | {% filter upper %} |
| 829 | This text becomes uppercase |
| 830 | {% endfilter %} |
| 831 | |
| 832 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 833 | .. _assignments: |
| 834 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 835 | Assignments |
| 836 | ~~~~~~~~~~~ |
| 837 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 838 | Inside code blocks, you can also assign values to variables. Assignments at |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 839 | top level (outside of blocks, macros or loops) are exported from the template |
| 840 | like top level macros and can be imported by other templates. |
| 841 | |
Armin Ronacher | 2e30cf5 | 2008-05-13 01:05:20 +0200 | [diff] [blame] | 842 | Assignments use the `set` tag and can have multiple targets:: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 843 | |
Armin Ronacher | 2e30cf5 | 2008-05-13 01:05:20 +0200 | [diff] [blame] | 844 | {% set navigation = [('index.html', 'Index'), ('about.html', 'About')] %} |
| 845 | {% set key, value = call_something() %} |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 846 | |
| 847 | |
Armin Ronacher | 3d91ae5 | 2014-06-07 00:58:38 +0600 | [diff] [blame] | 848 | Block Assignments |
| 849 | ~~~~~~~~~~~~~~~~~ |
| 850 | |
| 851 | .. versionadded:: 2.8 |
| 852 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 853 | Starting with Jinja 2.8, it's possible to also use block assignments to |
Armin Ronacher | 3d91ae5 | 2014-06-07 00:58:38 +0600 | [diff] [blame] | 854 | capture the contents of a block into a variable name. This can be useful |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 855 | in some situations as an alternative for macros. In that case, instead of |
| 856 | using an equals sign and a value, you just write the variable name and then |
Armin Ronacher | 3d91ae5 | 2014-06-07 00:58:38 +0600 | [diff] [blame] | 857 | everything until ``{% endset %}`` is captured. |
| 858 | |
| 859 | Example:: |
| 860 | |
| 861 | {% set navigation %} |
| 862 | <li><a href="/">Index</a> |
| 863 | <li><a href="/downloads">Downloads</a> |
| 864 | {% endset %} |
| 865 | |
| 866 | The `navigation` variable then contains the navigation HTML source. |
| 867 | |
| 868 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 869 | .. _extends: |
| 870 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 871 | Extends |
| 872 | ~~~~~~~ |
| 873 | |
Markus Unterwaditzer | 0ce1bca | 2015-03-11 21:12:47 +0100 | [diff] [blame] | 874 | The `extends` tag can be used to extend one template from another. You can |
| 875 | have multiple `extends` tags in a file, but only one of them may be executed at |
| 876 | a time. |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 877 | |
| 878 | See the section about :ref:`template-inheritance` above. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 879 | |
| 880 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 881 | .. _blocks: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 882 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 883 | Blocks |
| 884 | ~~~~~~ |
| 885 | |
| 886 | Blocks are used for inheritance and act as both placeholders and replacements |
| 887 | at the same time. They are documented in detail in the |
| 888 | :ref:`template-inheritance` section. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 889 | |
| 890 | |
| 891 | Include |
| 892 | ~~~~~~~ |
| 893 | |
| 894 | The `include` statement is useful to include a template and return the |
| 895 | rendered contents of that file into the current namespace:: |
| 896 | |
| 897 | {% include 'header.html' %} |
| 898 | Body |
| 899 | {% include 'footer.html' %} |
| 900 | |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 901 | Included templates have access to the variables of the active context by |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 902 | default. For more details about context behavior of imports and includes, |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 903 | see :ref:`import-visibility`. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 904 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 905 | From Jinja 2.2 onwards, you can mark an include with ``ignore missing``; in |
Dmitry Medvinsky | 200fe17 | 2012-09-21 17:15:38 +0400 | [diff] [blame] | 906 | which case Jinja will ignore the statement if the template to be included |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 907 | does not exist. When combined with ``with`` or ``without context``, it must |
| 908 | be placed *before* the context visibility statement. Here are some valid |
Armin Ronacher | 37f58ce | 2008-12-27 13:10:38 +0100 | [diff] [blame] | 909 | examples:: |
| 910 | |
| 911 | {% include "sidebar.html" ignore missing %} |
| 912 | {% include "sidebar.html" ignore missing with context %} |
| 913 | {% include "sidebar.html" ignore missing without context %} |
| 914 | |
Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 915 | .. versionadded:: 2.2 |
| 916 | |
| 917 | You can also provide a list of templates that are checked for existence |
| 918 | before inclusion. The first template that exists will be included. If |
| 919 | `ignore missing` is given, it will fall back to rendering nothing if |
| 920 | none of the templates exist, otherwise it will raise an exception. |
| 921 | |
| 922 | Example:: |
| 923 | |
| 924 | {% include ['page_detailed.html', 'page.html'] %} |
| 925 | {% include ['special_sidebar.html', 'sidebar.html'] ignore missing %} |
| 926 | |
Armin Ronacher | 9165d3e | 2010-02-16 17:35:59 +0100 | [diff] [blame] | 927 | .. versionchanged:: 2.4 |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 928 | If a template object was passed to the template context, you can |
Armin Ronacher | 9165d3e | 2010-02-16 17:35:59 +0100 | [diff] [blame] | 929 | include that object using `include`. |
| 930 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 931 | .. _import: |
| 932 | |
| 933 | Import |
| 934 | ~~~~~~ |
| 935 | |
| 936 | Jinja2 supports putting often used code into macros. These macros can go into |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 937 | different templates and get imported from there. This works similarly to the |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 938 | import statements in Python. It's important to know that imports are cached |
| 939 | and imported templates don't have access to the current template variables, |
Christoph Zwerschke | a21f2ed | 2011-10-23 18:23:58 +0200 | [diff] [blame] | 940 | just the globals by default. For more details about context behavior of |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 941 | imports and includes, see :ref:`import-visibility`. |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 942 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 943 | There are two ways to import templates. You can import a complete template |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 944 | into a variable or request specific macros / exported variables from it. |
| 945 | |
| 946 | Imagine we have a helper module that renders forms (called `forms.html`):: |
| 947 | |
| 948 | {% macro input(name, value='', type='text') -%} |
| 949 | <input type="{{ type }}" value="{{ value|e }}" name="{{ name }}"> |
| 950 | {%- endmacro %} |
| 951 | |
| 952 | {%- macro textarea(name, value='', rows=10, cols=40) -%} |
| 953 | <textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols |
| 954 | }}">{{ value|e }}</textarea> |
| 955 | {%- endmacro %} |
| 956 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 957 | The easiest and most flexible way to access a template's variables |
| 958 | and macros is to import the whole template module into a variable. |
| 959 | That way, you can access the attributes:: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 960 | |
| 961 | {% import 'forms.html' as forms %} |
| 962 | <dl> |
| 963 | <dt>Username</dt> |
| 964 | <dd>{{ forms.input('username') }}</dd> |
| 965 | <dt>Password</dt> |
| 966 | <dd>{{ forms.input('password', type='password') }}</dd> |
| 967 | </dl> |
| 968 | <p>{{ forms.textarea('comment') }}</p> |
| 969 | |
| 970 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 971 | Alternatively, you can import specific names from a template into the current |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 972 | namespace:: |
| 973 | |
| 974 | {% from 'forms.html' import input as input_field, textarea %} |
| 975 | <dl> |
| 976 | <dt>Username</dt> |
| 977 | <dd>{{ input_field('username') }}</dd> |
| 978 | <dt>Password</dt> |
| 979 | <dd>{{ input_field('password', type='password') }}</dd> |
| 980 | </dl> |
| 981 | <p>{{ textarea('comment') }}</p> |
| 982 | |
Natan L | 63bd806 | 2012-11-22 00:10:41 -0800 | [diff] [blame] | 983 | Macros and variables starting with one or more underscores are private and |
Armin Ronacher | 903d168 | 2008-05-23 00:51:58 +0200 | [diff] [blame] | 984 | cannot be imported. |
| 985 | |
Armin Ronacher | 9165d3e | 2010-02-16 17:35:59 +0100 | [diff] [blame] | 986 | .. versionchanged:: 2.4 |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 987 | If a template object was passed to the template context, you can |
Armin Ronacher | 9165d3e | 2010-02-16 17:35:59 +0100 | [diff] [blame] | 988 | import from that object. |
| 989 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 990 | |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 991 | .. _import-visibility: |
| 992 | |
| 993 | Import Context Behavior |
| 994 | ----------------------- |
| 995 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 996 | By default, included templates are passed the current context and imported |
| 997 | templates are not. The reason for this is that imports, unlike includes, |
| 998 | are cached; as imports are often used just as a module that holds macros. |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 999 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1000 | This behavior can be changed explicitly: by adding `with context` |
| 1001 | or `without context` to the import/include directive, the current context |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 1002 | can be passed to the template and caching is disabled automatically. |
| 1003 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1004 | Here are two examples:: |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 1005 | |
| 1006 | {% from 'forms.html' import input with context %} |
| 1007 | {% include 'header.html' without context %} |
| 1008 | |
Armin Ronacher | 673aa88 | 2008-10-04 18:06:57 +0200 | [diff] [blame] | 1009 | .. admonition:: Note |
| 1010 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1011 | In Jinja 2.0, the context that was passed to the included template |
Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 1012 | did not include variables defined in the template. As a matter of |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1013 | fact, this did not work:: |
Armin Ronacher | 673aa88 | 2008-10-04 18:06:57 +0200 | [diff] [blame] | 1014 | |
| 1015 | {% for box in boxes %} |
| 1016 | {% include "render_box.html" %} |
| 1017 | {% endfor %} |
| 1018 | |
Alan Hogan | 02901a8 | 2011-04-28 20:18:55 -0700 | [diff] [blame] | 1019 | The included template ``render_box.html`` is *not* able to access |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1020 | `box` in Jinja 2.0. As of Jinja 2.1, ``render_box.html`` *is* able |
Alan Hogan | 02901a8 | 2011-04-28 20:18:55 -0700 | [diff] [blame] | 1021 | to do so. |
Armin Ronacher | 673aa88 | 2008-10-04 18:06:57 +0200 | [diff] [blame] | 1022 | |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 1023 | |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1024 | .. _expressions: |
| 1025 | |
| 1026 | Expressions |
| 1027 | ----------- |
| 1028 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1029 | Jinja allows basic expressions everywhere. These work very similarly to |
| 1030 | regular Python; even if you're not working with Python |
| 1031 | you should feel comfortable with it. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1032 | |
| 1033 | Literals |
| 1034 | ~~~~~~~~ |
| 1035 | |
| 1036 | The simplest form of expressions are literals. Literals are representations |
| 1037 | for Python objects such as strings and numbers. The following literals exist: |
| 1038 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1039 | "Hello World": |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1040 | Everything between two double or single quotes is a string. They are |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1041 | useful whenever you need a string in the template (e.g. as |
| 1042 | arguments to function calls and filters, or just to extend or include a |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1043 | template). |
| 1044 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1045 | 42 / 42.23: |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1046 | Integers and floating point numbers are created by just writing the |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1047 | number down. If a dot is present, the number is a float, otherwise an |
| 1048 | integer. Keep in mind that, in Python, ``42`` and ``42.0`` |
| 1049 | are different (``int`` and ``float``, respectively). |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1050 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1051 | ['list', 'of', 'objects']: |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1052 | Everything between two brackets is a list. Lists are useful for storing |
| 1053 | sequential data to be iterated over. For example, you can easily |
| 1054 | create a list of links using lists and tuples for (and with) a for loop:: |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1055 | |
| 1056 | <ul> |
| 1057 | {% for href, caption in [('index.html', 'Index'), ('about.html', 'About'), |
| 1058 | ('downloads.html', 'Downloads')] %} |
| 1059 | <li><a href="{{ href }}">{{ caption }}</a></li> |
| 1060 | {% endfor %} |
| 1061 | </ul> |
| 1062 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1063 | ('tuple', 'of', 'values'): |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1064 | Tuples are like lists that cannot be modified ("immutable"). If a tuple |
| 1065 | only has one item, it must be followed by a comma (``('1-tuple',)``). |
| 1066 | Tuples are usually used to represent items of two or more elements. |
| 1067 | See the list example above for more details. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1068 | |
Lukas Meuser | ad48a2e | 2008-05-01 18:19:57 +0200 | [diff] [blame] | 1069 | {'dict': 'of', 'key': 'and', 'value': 'pairs'}: |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1070 | A dict in Python is a structure that combines keys and values. Keys must |
| 1071 | be unique and always have exactly one value. Dicts are rarely used in |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1072 | templates; they are useful in some rare cases such as the :func:`xmlattr` |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1073 | filter. |
| 1074 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1075 | true / false: |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 1076 | true is always true and false is always false. |
| 1077 | |
| 1078 | .. admonition:: Note |
| 1079 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1080 | The special constants `true`, `false`, and `none` are indeed lowercase. |
| 1081 | Because that caused confusion in the past, (`True` used to expand |
| 1082 | to an undefined variable that was considered false), |
| 1083 | all three can now also be written in title case |
| 1084 | (`True`, `False`, and `None`). |
| 1085 | However, for consistency, (all Jinja identifiers are lowercase) |
| 1086 | you should use the lowercase versions. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1087 | |
| 1088 | Math |
| 1089 | ~~~~ |
| 1090 | |
| 1091 | Jinja allows you to calculate with values. This is rarely useful in templates |
Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 1092 | but exists for completeness' sake. The following operators are supported: |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1093 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1094 | \+ |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1095 | Adds two objects together. Usually the objects are numbers, but if both are |
| 1096 | strings or lists, you can concatenate them this way. This, however, is not |
| 1097 | the preferred way to concatenate strings! For string concatenation, have |
| 1098 | a look-see at the ``~`` operator. ``{{ 1 + 1 }}`` is ``2``. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1099 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1100 | \- |
Jakub Wilk | 3fc008b | 2013-05-25 23:37:34 +0200 | [diff] [blame] | 1101 | Subtract the second number from the first one. ``{{ 3 - 2 }}`` is ``1``. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1102 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1103 | / |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1104 | Divide two numbers. The return value will be a floating point number. |
| 1105 | ``{{ 1 / 2 }}`` is ``{{ 0.5 }}``. |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1106 | (Just like ``from __future__ import division``.) |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1107 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1108 | // |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1109 | Divide two numbers and return the truncated integer result. |
Fabian Topfstedt | 6ae1285 | 2011-05-22 08:19:12 -0700 | [diff] [blame] | 1110 | ``{{ 20 // 7 }}`` is ``2``. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1111 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1112 | % |
Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 1113 | Calculate the remainder of an integer division. ``{{ 11 % 7 }}`` is ``4``. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1114 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1115 | \* |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1116 | Multiply the left operand with the right one. ``{{ 2 * 2 }}`` would |
Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 1117 | return ``4``. This can also be used to repeat a string multiple times. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1118 | ``{{ '=' * 80 }}`` would print a bar of 80 equal signs. |
| 1119 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1120 | \** |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1121 | Raise the left operand to the power of the right operand. ``{{ 2**3 }}`` |
| 1122 | would return ``8``. |
| 1123 | |
Armin Ronacher | 3c95532 | 2010-08-08 22:23:51 +0200 | [diff] [blame] | 1124 | Comparisons |
| 1125 | ~~~~~~~~~~~ |
Armin Ronacher | 13203e1 | 2010-08-08 21:59:29 +0200 | [diff] [blame] | 1126 | |
| 1127 | == |
| 1128 | Compares two objects for equality. |
| 1129 | |
| 1130 | != |
| 1131 | Compares two objects for inequality. |
| 1132 | |
| 1133 | > |
| 1134 | `true` if the left hand side is greater than the right hand side. |
| 1135 | |
| 1136 | >= |
| 1137 | `true` if the left hand side is greater or equal to the right hand side. |
| 1138 | |
| 1139 | < |
| 1140 | `true` if the left hand side is lower than the right hand side. |
| 1141 | |
| 1142 | <= |
| 1143 | `true` if the left hand side is lower or equal to the right hand side. |
| 1144 | |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1145 | Logic |
| 1146 | ~~~~~ |
| 1147 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1148 | For `if` statements, `for` filtering, and `if` expressions, it can be useful to |
Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 1149 | combine multiple expressions: |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1150 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1151 | and |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1152 | Return true if the left and the right operand are true. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1153 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1154 | or |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1155 | Return true if the left or the right operand are true. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1156 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1157 | not |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1158 | negate a statement (see below). |
| 1159 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1160 | (expr) |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1161 | group an expression. |
| 1162 | |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 1163 | .. admonition:: Note |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1164 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1165 | The ``is`` and ``in`` operators support negation using an infix notation, |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 1166 | too: ``foo is not bar`` and ``foo not in bar`` instead of ``not foo is bar`` |
| 1167 | and ``not foo in bar``. All other expressions require a prefix notation: |
| 1168 | ``not (foo and bar).`` |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1169 | |
| 1170 | |
| 1171 | Other Operators |
| 1172 | ~~~~~~~~~~~~~~~ |
| 1173 | |
| 1174 | The following operators are very useful but don't fit into any of the other |
| 1175 | two categories: |
| 1176 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1177 | in |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1178 | Perform a sequence / mapping containment test. Returns true if the left |
| 1179 | operand is contained in the right. ``{{ 1 in [1, 2, 3] }}`` would, for |
| 1180 | example, return true. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1181 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1182 | is |
| 1183 | Performs a :ref:`test <tests>`. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1184 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1185 | \| |
| 1186 | Applies a :ref:`filter <filters>`. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1187 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1188 | ~ |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1189 | Converts all operands into strings and concatenates them. |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1190 | |
| 1191 | ``{{ "Hello " ~ name ~ "!" }}`` would return (assuming `name` is set |
| 1192 | to ``'John'``) ``Hello John!``. |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1193 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1194 | () |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1195 | Call a callable: ``{{ post.render() }}``. Inside of the parentheses you |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1196 | can use positional arguments and keyword arguments like in Python: |
| 1197 | |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1198 | ``{{ post.render(user, full=true) }}``. |
| 1199 | |
Armin Ronacher | 316157d | 2008-04-28 18:30:27 +0200 | [diff] [blame] | 1200 | . / [] |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1201 | Get an attribute of an object. (See :ref:`variables`) |
| 1202 | |
| 1203 | |
| 1204 | .. _if-expression: |
| 1205 | |
| 1206 | If Expression |
| 1207 | ~~~~~~~~~~~~~ |
| 1208 | |
Armin Ronacher | b2a36aa | 2008-04-28 19:57:40 +0200 | [diff] [blame] | 1209 | It is also possible to use inline `if` expressions. These are useful in some |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1210 | situations. For example, you can use this to extend from one template if a |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 1211 | variable is defined, otherwise from the default layout template:: |
Armin Ronacher | b2a36aa | 2008-04-28 19:57:40 +0200 | [diff] [blame] | 1212 | |
| 1213 | {% extends layout_template if layout_template is defined else 'master.html' %} |
Armin Ronacher | 709f6e5 | 2008-04-28 18:18:16 +0200 | [diff] [blame] | 1214 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 1215 | The general syntax is ``<do something> if <something is true> else <do |
| 1216 | something else>``. |
| 1217 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1218 | The `else` part is optional. If not provided, the else block implicitly |
Armin Ronacher | a0e3ac0 | 2008-07-04 17:00:38 +0200 | [diff] [blame] | 1219 | evaluates into an undefined object:: |
| 1220 | |
| 1221 | {{ '[%s]' % page.title if page.title }} |
| 1222 | |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 1223 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 1224 | .. _builtin-filters: |
| 1225 | |
| 1226 | List of Builtin Filters |
| 1227 | ----------------------- |
| 1228 | |
| 1229 | .. jinjafilters:: |
| 1230 | |
| 1231 | |
| 1232 | .. _builtin-tests: |
| 1233 | |
| 1234 | List of Builtin Tests |
| 1235 | --------------------- |
| 1236 | |
Armin Ronacher | 157531b | 2008-04-28 16:14:03 +0200 | [diff] [blame] | 1237 | .. jinjatests:: |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 1238 | |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 1239 | .. _builtin-globals: |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 1240 | |
| 1241 | List of Global Functions |
| 1242 | ------------------------ |
| 1243 | |
| 1244 | The following functions are available in the global scope by default: |
| 1245 | |
| 1246 | .. function:: range([start,] stop[, step]) |
| 1247 | |
| 1248 | Return a list containing an arithmetic progression of integers. |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1249 | ``range(i, j)`` returns ``[i, i+1, i+2, ..., j-1]``; |
| 1250 | start (!) defaults to ``0``. |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 1251 | When step is given, it specifies the increment (or decrement). |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1252 | For example, ``range(4)`` and ``range(0, 4, 1)`` return ``[0, 1, 2, 3]``. |
| 1253 | The end point is omitted! |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 1254 | These are exactly the valid indices for a list of 4 elements. |
| 1255 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1256 | This is useful to repeat a template block multiple times, e.g. |
Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 1257 | to fill a list. Imagine you have 7 users in the list but you want to |
| 1258 | render three empty items to enforce a height with CSS:: |
| 1259 | |
| 1260 | <ul> |
| 1261 | {% for user in users %} |
| 1262 | <li>{{ user.username }}</li> |
| 1263 | {% endfor %} |
| 1264 | {% for number in range(10 - users|count) %} |
| 1265 | <li class="empty"><span>...</span></li> |
| 1266 | {% endfor %} |
| 1267 | </ul> |
| 1268 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 1269 | .. function:: lipsum(n=5, html=True, min=20, max=100) |
| 1270 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1271 | Generates some lorem ipsum for the template. By default, five paragraphs |
| 1272 | of HTML are generated with each paragraph between 20 and 100 words. |
| 1273 | If html is False, regular text is returned. This is useful to generate simple |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 1274 | contents for layout testing. |
Armin Ronacher | 76c280b | 2008-05-04 12:31:48 +0200 | [diff] [blame] | 1275 | |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1276 | .. function:: dict(\**items) |
Armin Ronacher | 76c280b | 2008-05-04 12:31:48 +0200 | [diff] [blame] | 1277 | |
| 1278 | A convenient alternative to dict literals. ``{'foo': 'bar'}`` is the same |
| 1279 | as ``dict(foo='bar')``. |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1280 | |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 1281 | .. class:: cycler(\*items) |
| 1282 | |
| 1283 | The cycler allows you to cycle among values similar to how `loop.cycle` |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1284 | works. Unlike `loop.cycle`, you can use this cycler outside of |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 1285 | loops or over multiple loops. |
| 1286 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1287 | This can be very useful if you want to show a list of folders and |
| 1288 | files with the folders on top but both in the same list with alternating |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 1289 | row colors. |
| 1290 | |
| 1291 | The following example shows how `cycler` can be used:: |
| 1292 | |
| 1293 | {% set row_class = cycler('odd', 'even') %} |
| 1294 | <ul class="browser"> |
| 1295 | {% for folder in folders %} |
| 1296 | <li class="folder {{ row_class.next() }}">{{ folder|e }}</li> |
| 1297 | {% endfor %} |
| 1298 | {% for filename in files %} |
| 1299 | <li class="file {{ row_class.next() }}">{{ filename|e }}</li> |
| 1300 | {% endfor %} |
| 1301 | </ul> |
| 1302 | |
| 1303 | A cycler has the following attributes and methods: |
| 1304 | |
| 1305 | .. method:: reset() |
| 1306 | |
| 1307 | Resets the cycle to the first item. |
| 1308 | |
| 1309 | .. method:: next() |
| 1310 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1311 | Goes one item ahead and returns the then-current item. |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 1312 | |
| 1313 | .. attribute:: current |
| 1314 | |
| 1315 | Returns the current item. |
Andy McKay | 2004449 | 2011-06-30 14:40:05 -0700 | [diff] [blame] | 1316 | |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 1317 | **new in Jinja 2.1** |
| 1318 | |
Armin Ronacher | d34eb12 | 2008-10-13 23:47:51 +0200 | [diff] [blame] | 1319 | .. class:: joiner(sep=', ') |
| 1320 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1321 | A tiny helper that can be used to "join" multiple sections. A joiner is |
Kevin Schuetz | bc542b0 | 2011-07-26 14:16:46 -0500 | [diff] [blame] | 1322 | passed a string and will return that string every time it's called, except |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1323 | the first time (in which case it returns an empty string). You can |
Armin Ronacher | d34eb12 | 2008-10-13 23:47:51 +0200 | [diff] [blame] | 1324 | use this to join things:: |
| 1325 | |
| 1326 | {% set pipe = joiner("|") %} |
| 1327 | {% if categories %} {{ pipe() }} |
| 1328 | Categories: {{ categories|join(", ") }} |
| 1329 | {% endif %} |
| 1330 | {% if author %} {{ pipe() }} |
| 1331 | Author: {{ author() }} |
| 1332 | {% endif %} |
| 1333 | {% if can_edit %} {{ pipe() }} |
| 1334 | <a href="?action=edit">Edit</a> |
| 1335 | {% endif %} |
| 1336 | |
| 1337 | **new in Jinja 2.1** |
| 1338 | |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1339 | |
| 1340 | Extensions |
| 1341 | ---------- |
| 1342 | |
| 1343 | The following sections cover the built-in Jinja2 extensions that may be |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1344 | enabled by an application. An application could also provide further |
| 1345 | extensions not covered by this documentation; in which case there should |
| 1346 | be a separate document explaining said :ref:`extensions |
| 1347 | <jinja-extensions>`. |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1348 | |
| 1349 | .. _i18n-in-templates: |
| 1350 | |
| 1351 | i18n |
| 1352 | ~~~~ |
| 1353 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1354 | If the i18n extension is enabled, it's possible to mark parts in the template |
| 1355 | as translatable. To mark a section as translatable, you can use `trans`:: |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1356 | |
| 1357 | <p>{% trans %}Hello {{ user }}!{% endtrans %}</p> |
| 1358 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1359 | To translate a template expression --- say, using template filters, or by just |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1360 | accessing an attribute of an object --- you need to bind the expression to a |
| 1361 | name for use within the translation block:: |
| 1362 | |
| 1363 | <p>{% trans user=user.username %}Hello {{ user }}!{% endtrans %}</p> |
| 1364 | |
| 1365 | If you need to bind more than one expression inside a `trans` tag, separate |
| 1366 | the pieces with a comma (``,``):: |
| 1367 | |
| 1368 | {% trans book_title=book.title, author=author.name %} |
| 1369 | This is {{ book_title }} by {{ author }} |
| 1370 | {% endtrans %} |
| 1371 | |
| 1372 | Inside trans tags no statements are allowed, only variable tags are. |
| 1373 | |
| 1374 | To pluralize, specify both the singular and plural forms with the `pluralize` |
| 1375 | tag, which appears between `trans` and `endtrans`:: |
| 1376 | |
| 1377 | {% trans count=list|length %} |
| 1378 | There is {{ count }} {{ name }} object. |
| 1379 | {% pluralize %} |
| 1380 | There are {{ count }} {{ name }} objects. |
| 1381 | {% endtrans %} |
| 1382 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1383 | By default, the first variable in a block is used to determine the correct |
| 1384 | singular or plural form. If that doesn't work out, you can specify the name |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1385 | which should be used for pluralizing by adding it as parameter to `pluralize`:: |
| 1386 | |
| 1387 | {% trans ..., user_count=users|length %}... |
| 1388 | {% pluralize user_count %}...{% endtrans %} |
| 1389 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1390 | It's also possible to translate strings in expressions. For that purpose, |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1391 | three functions exist: |
| 1392 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1393 | - `gettext`: translate a single string |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1394 | - `ngettext`: translate a pluralizable string |
| 1395 | - `_`: alias for `gettext` |
| 1396 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1397 | For example, you can easily print a translated string like this:: |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1398 | |
| 1399 | {{ _('Hello World!') }} |
| 1400 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1401 | To use placeholders, use the `format` filter:: |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1402 | |
| 1403 | {{ _('Hello %(user)s!')|format(user=user.username) }} |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 1404 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1405 | For multiple placeholders, always use keyword arguments to `format`, |
| 1406 | as other languages may not use the words in the same order. |
Armin Ronacher | 5d2733f | 2008-05-15 23:26:52 +0200 | [diff] [blame] | 1407 | |
Armin Ronacher | ffaa2e7 | 2010-05-29 20:57:16 +0200 | [diff] [blame] | 1408 | .. versionchanged:: 2.5 |
| 1409 | |
Armin Ronacher | 8a3d93b | 2010-05-29 22:02:03 +0200 | [diff] [blame] | 1410 | If newstyle gettext calls are activated (:ref:`newstyle-gettext`), using |
Armin Ronacher | ffaa2e7 | 2010-05-29 20:57:16 +0200 | [diff] [blame] | 1411 | placeholders is a lot easier: |
| 1412 | |
| 1413 | .. sourcecode:: html+jinja |
| 1414 | |
| 1415 | {{ gettext('Hello World!') }} |
| 1416 | {{ gettext('Hello %(name)s!', name='World') }} |
| 1417 | {{ ngettext('%(num)d apple', '%(num)d apples', apples|count) }} |
| 1418 | |
Kevin Schuetz | 522af5d | 2011-07-26 14:17:38 -0500 | [diff] [blame] | 1419 | Note that the `ngettext` function's format string automatically receives |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1420 | the count as a `num` parameter in addition to the regular parameters. |
Armin Ronacher | ffaa2e7 | 2010-05-29 20:57:16 +0200 | [diff] [blame] | 1421 | |
Armin Ronacher | 5d2733f | 2008-05-15 23:26:52 +0200 | [diff] [blame] | 1422 | |
Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 1423 | Expression Statement |
| 1424 | ~~~~~~~~~~~~~~~~~~~~ |
Armin Ronacher | 5d2733f | 2008-05-15 23:26:52 +0200 | [diff] [blame] | 1425 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1426 | If the expression-statement extension is loaded, a tag called `do` is available |
| 1427 | that works exactly like the regular variable expression (``{{ ... }}``); except |
| 1428 | it doesn't print anything. This can be used to modify lists:: |
Armin Ronacher | 5d2733f | 2008-05-15 23:26:52 +0200 | [diff] [blame] | 1429 | |
| 1430 | {% do navigation.append('a string') %} |
Armin Ronacher | 3da9031 | 2008-05-23 16:37:28 +0200 | [diff] [blame] | 1431 | |
| 1432 | |
| 1433 | Loop Controls |
| 1434 | ~~~~~~~~~~~~~ |
| 1435 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1436 | If the application enables the :ref:`loopcontrols-extension`, it's possible to |
Armin Ronacher | 3da9031 | 2008-05-23 16:37:28 +0200 | [diff] [blame] | 1437 | use `break` and `continue` in loops. When `break` is reached, the loop is |
Kevin Schuetz | c585550 | 2011-07-26 14:21:52 -0500 | [diff] [blame] | 1438 | terminated; if `continue` is reached, the processing is stopped and continues |
Armin Ronacher | 3da9031 | 2008-05-23 16:37:28 +0200 | [diff] [blame] | 1439 | with the next iteration. |
| 1440 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1441 | Here's a loop that skips every second item:: |
Armin Ronacher | 3da9031 | 2008-05-23 16:37:28 +0200 | [diff] [blame] | 1442 | |
| 1443 | {% for user in users %} |
| 1444 | {%- if loop.index is even %}{% continue %}{% endif %} |
| 1445 | ... |
| 1446 | {% endfor %} |
| 1447 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1448 | Likewise, a loop that stops processing after the 10th iteration:: |
Armin Ronacher | 3da9031 | 2008-05-23 16:37:28 +0200 | [diff] [blame] | 1449 | |
| 1450 | {% for user in users %} |
| 1451 | {%- if loop.index >= 10 %}{% break %}{% endif %} |
| 1452 | {%- endfor %} |
Armin Ronacher | 9b4cc9f | 2010-02-07 03:55:15 +0100 | [diff] [blame] | 1453 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1454 | Note that ``loop.index`` starts with 1, and ``loop.index0`` starts with 0 |
| 1455 | (See: :ref:`for-loop`). |
| 1456 | |
Armin Ronacher | 9b4cc9f | 2010-02-07 03:55:15 +0100 | [diff] [blame] | 1457 | |
| 1458 | With Statement |
| 1459 | ~~~~~~~~~~~~~~ |
| 1460 | |
| 1461 | .. versionadded:: 2.3 |
| 1462 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1463 | If the application enables the :ref:`with-extension`, it is possible to |
Armin Ronacher | 9b4cc9f | 2010-02-07 03:55:15 +0100 | [diff] [blame] | 1464 | use the `with` keyword in templates. This makes it possible to create |
| 1465 | a new inner scope. Variables set within this scope are not visible |
| 1466 | outside of the scope. |
| 1467 | |
| 1468 | With in a nutshell:: |
| 1469 | |
| 1470 | {% with %} |
| 1471 | {% set foo = 42 %} |
| 1472 | {{ foo }} foo is 42 here |
| 1473 | {% endwith %} |
| 1474 | foo is not visible here any longer |
| 1475 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1476 | Because it is common to set variables at the beginning of the scope, |
| 1477 | you can do that within the `with` statement. The following two examples |
Armin Ronacher | 9b4cc9f | 2010-02-07 03:55:15 +0100 | [diff] [blame] | 1478 | are equivalent:: |
| 1479 | |
| 1480 | {% with foo = 42 %} |
| 1481 | {{ foo }} |
| 1482 | {% endwith %} |
| 1483 | |
| 1484 | {% with %} |
| 1485 | {% set foo = 42 %} |
| 1486 | {{ foo }} |
| 1487 | {% endwith %} |
Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 1488 | |
Armin Ronacher | 752ba7f | 2010-04-05 18:17:27 +0200 | [diff] [blame] | 1489 | .. _autoescape-overrides: |
Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 1490 | |
| 1491 | Autoescape Extension |
| 1492 | -------------------- |
| 1493 | |
| 1494 | .. versionadded:: 2.4 |
| 1495 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1496 | If the application enables the :ref:`autoescape-extension`, one can |
Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 1497 | activate and deactivate the autoescaping from within the templates. |
| 1498 | |
| 1499 | Example:: |
| 1500 | |
| 1501 | {% autoescape true %} |
| 1502 | Autoescaping is active within this block |
| 1503 | {% endautoescape %} |
| 1504 | |
| 1505 | {% autoescape false %} |
| 1506 | Autoescaping is inactive within this block |
| 1507 | {% endautoescape %} |
| 1508 | |
Wes Turner | d40e9ba | 2014-12-11 05:24:53 -0600 | [diff] [blame] | 1509 | After an `endautoescape` the behavior is reverted to what it was before. |