Skip to content
Myr

[svn] Proofread designerdoc.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Georg Brandl · committed by Georg Brandl · parent 4e0cd08 · Browse files · 1 file+84 added−75 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+84 added−75 removed

  • other:+84 added−75 removed

1 file+84 added−75 removed

modified:docs/src/designerdoc.txt+84 added-75 removed
44 unmodified lines
45the statement ``{% print foobar %}``.4647Variables are coming from the context provided by the application. Normally there48should be a documentation regarding the context contents but if you want to know49the content of the current context you can add this to your template:49the content of the current context, you can add this to your template:5051.. sourcecode:: html+jinja5253 <pre>{{ debug()|e }}</pre>5455A context isn't flat which means that each variable can has subvariables, as long56as it is representable as python data structure. You can access attributes of57a variable by using the dot and brace operators. The following examples show57a variable using the dot and bracket operators. The following examples show58this:5960.. sourcecode:: jinja61
8 unmodified lines
70Filters71=======7273In the examples above you might have noticed the pipe symbols. Pipe symbols tell74the engine that it has to apply a filter on the variable. Here a small example:74the engine that it has to apply a filter on the variable. Here is a small example:7576.. sourcecode:: jinja7778 {{ variable|replace('foo', 'bar')|escape }}7980If you like you can also put whitespace between the filters.80If you want, you can also put whitespace between the filters.8182This will look for a variable variable, passes it to the filter replace with the83arguments ``'foo'`` and ``'bar'``, and passes the result to the filter `escape`84that automatically xml escapes the value. The ``e`` filter is an alias for85``escape``. Here the complete list of supported filters:82This will look for a variable `variable`, pass it to the filter `replace`83with the arguments ``'foo'`` and ``'bar'``, and pass the result to the filter84`escape` that automatically XML-escapes the value. The `e` filter is an alias for85`escape`. Here is the complete list of supported filters:8687[[list_of_filters]]8889.. admonition:: note9091 Filters have a pretty low priority. If you want to add fitered values92 you have to put them into parenthesis. Also you you want to access92 you have to put them into parentheses. The same applies if you want to access93 attributes:9495 .. sourcecode:: jinja96
9 unmodified lines
106107Tests108=====109110You can use the `is`-operator to perform tests on a value:110You can use the `is` operator to perform tests on a value:111112.. sourcecode:: jinja113114 {{ 42 is numeric }} -> true115 {{ "foobar" is numeric }} -> false116 {{ 'FOO' is upper }} -> true117118Those tests are especially useful if used in `if`-conditions.118These tests are especially useful when used in `if` conditions.119120[[list_of_tests]]121122Loops123=====124125To iterate over a sequence you can use the `for`-loop. If basically looks like a126normal python for loop and works pretty much the same:125To iterate over a sequence, you can use the `for` loop. It basically looks like a126normal Python `for` loop and works pretty much the same:127128.. sourcecode:: html+jinja129130 <h1>Members</h1>
4 unmodified lines
135 <li><em>no users found</em></li>136 {% endfor %}137 </ul>138139The optional ``else`` block is only executed if the template did not iterate140because the sequence was empty.139*Important difference from Python:* the optional ``else`` block is only140executed if there was no iteration because the sequence was empty.141142Inside of a for loop block you can access some special variables:142Inside of a `for` loop block you can access some special variables:143144+----------------------+----------------------------------------+145| Variable | Description |146+======================+========================================+147| ``loop.index`` | The current iteration of the loop. |147| `loop.index` | The current iteration of the loop. |148+----------------------+----------------------------------------+149| ``loop.index0`` | The current iteration of the loop, |149| `loop.index0` | The current iteration of the loop, |150| | starting counting by 0. |151+----------------------+----------------------------------------+152| ``loop.revindex`` | The number of iterations from the end |152| `loop.revindex` | The number of iterations from the end |153| | of the loop. |154+----------------------+----------------------------------------+155| ``loop.revindex0`` | The number of iterations from the end |155| `loop.revindex0` | The number of iterations from the end |156| | of the loop, starting counting by 0. |157+----------------------+----------------------------------------+158| ``loop.first`` | True if first iteration. |158| `loop.first` | True if first iteration. |159+----------------------+----------------------------------------+160| ``loop.last`` | True if last iteration. |160| `loop.last` | True if last iteration. |161+----------------------+----------------------------------------+162| ``loop.even`` | True if current iteration is even. |162| `loop.even` | True if current iteration is even. |163+----------------------+----------------------------------------+164| ``loop.odd`` | True if current iteration is odd. |164| `loop.odd` | True if current iteration is odd. |165+----------------------+----------------------------------------+166| ``loop.length`` | Total number of items in the sequence. |166| `loop.length` | Total number of items in the sequence. |167+----------------------+----------------------------------------+168| ``loop.parent`` | The context of the parent loop. |168| `loop.parent` | The context of the parent loop. |169+----------------------+----------------------------------------+170171Loops also support recursion. For example you have a sitemap where each item172might have a number of child items. Such a template could look like this:171Loops also support recursion. Let's assume you have a sitemap where each item172might have a number of child items. A template for that could look like this:173174.. sourcecode:: html+jinja175176 <h1>Sitemap
3 unmodified lines
180 {% if item.children %}<ul>{{ loop(item.children) }}</ul>{% endif %}</li>181 {% endfor %}182 </ul>183184Now. What happens here? Basically the first thing that is different to a normal184What happens here? Basically the first thing that is different to a normal185loop is the additional ``recursive`` modifier in the `for`-loop declaration.186It tells the template engine that we want recursion. If recursion is enabled187the special loop variable is callable. If you call it with a sequence it will188automatically render that loop at that position with the new sequence as argument.187the special `loop` variable is callable. If you call it with a sequence it will188automatically render the loop at that position with the new sequence as argument.189190Cycling191=======192193Sometimes you might want to have different classes for each row in a list. For194example to have alternating row colors. You can easily do this by using the193Sometimes you might want to have different text snippets for each row in a list,194for example to have alternating row colors. You can easily do this by using the195``{% cycle %}`` tag:196197.. sourcecode:: html+jinja198
2 unmodified lines
201 <li class="{% cycle 'row1', 'row2' %}">{{ message|e }}</li>202 {% endfor %}203 </ul>204205Each time Jinja encounters a cycle tag it will evaluate cycle through the list205Each time Jinja encounters a `cycle` tag it will cycle through the list206of given items and return the next one. If you pass it one item jinja assumes207that this item is a sequence from the context and uses this:208209.. sourcecode:: html+jinja
2 unmodified lines
212213Conditions214==========215216Jinja supports python like ``if`` / ``elif`` / ``else`` constructs:216Jinja supports Python-like `if` / `elif` / `else` constructs:217218.. sourcecode:: jinja219220 {% if user.active %}
18 unmodified lines
239 {% endif %}240241.. admonition:: Note242243 Of course you can use `or` / `and` and parenthesis to create more complex244 conditions but usually the logic is already handled in the application and245 you don't have to create such complex constucts in the template code. However243 Of course you can use `or` / `and` and parentheses to create more complex244 conditions, but usually the logic is already handled in the application and245 you don't have to create such complex constructs in the template code. However246 in some situations it might be a good thing to have the abilities to create247 them.248249Operators250=========251252Inside ``{{ variable }}`` blocks, `if`-conditions and many other parts you can253can use Expressions. In expressions you can use any of the following operators:252Inside ``{{ variable }}`` blocks, `if` conditions and many other parts you can253can use expressions. In expressions you can use any of the following operators:254255 ======= ===================================================================256 ``+`` add the right operand to the left one.257 ``{{ 1 + 2 }}`` would return three.258 ``-`` substract the right operand from the left one.259 ``{{ 1 - 1 }}`` would return zero.260 ``/`` divide the right from the left operand.261 ``{{ 1 / 2 }}`` would return 0.5262 ``*`` multiply the left operand with the right.263 ``{{ 2 * 2}}`` would return 4257 ``{{ 1 + 2 }}`` would return ``3``.258 ``-`` subtract the right operand from the left one.259 ``{{ 1 - 1 }}`` would return ``0``.260 ``/`` divide the left operand by the right one.261 ``{{ 1 / 2 }}`` would return ``0.5``.262 ``*`` multiply the left operand with the right one.263 ``{{ 2 * 2 }}`` would return ``4``.264 ``**`` raise the left operand to the power of the right265 operand. ``{{ 2**3 }}`` would return 8265 operand. ``{{ 2**3 }}`` would return ``8``.266 ``in`` perform sequence membership test. ``{{ 1 in [1,2,3] }}`` would267 return true.268 ``is`` perform a test on the value. See the section about269 tests for more information.270 ``|`` apply a filter on the value. See the section about271 filters for more information.272 ``and`` return true if the left and the right operand is true.273 ``or`` return true if the left or the right operand is true.272 ``not`` negate an statement *273 ``()`` call a callable. ``{{ user.get_username() }}``. Inside of the274 parenthesis you can use variables: ``{{ user.get('username') }}``.274 ``not`` negate a statement (see below)275 ``()`` call a callable: ``{{ user.get_username() }}``. Inside of the276 parentheses you can use variables: ``{{ user.get(username) }}``.277 ======= ===================================================================278277Note that there is no support for any bit operation or something similar.279Note that there is no support for any bit operations or something similar.280279* special note regarding `not`. The `is` and `in` operators support negation281* special note regarding `not`: The `is` and `in` operators support negation282 using an infix notation too: ``foo is not bar`` and ``foo not in bar``283 instead of ``not foo is bar`` and ``not foo in bar``. All other expressions284 require a prefix notation: ``not (foo and bar)``.285286Macros287======288287If you want to use a partial template on more than one place you might want to288create a macro out of it:289If you want to use a partial template in more than one place, you might want to290create a macro from it:291292.. sourcecode:: html+jinja293294 {% macro show_user user %}
28 unmodified lines
323===========324325The most powerful part of Jinja is template inheritance. Template inheritance326allows you to build a base "skeleton" template that contains all the common325elements of your site and defines **blocks** that child326templates can override.327elements of your site and defines **blocks** that child templates can override.328329Sounds complicated but is very basic. It's easiest to understand it by starting330with an example.331
26 unmodified lines
358 </div>359 </body>360361In this example, the ``{% block %}`` tags define four blocks that child templates361can fill in. All the ``block`` tag does is to tell the template engine that a362can fill in. All the `block` tag does is to tell the template engine that a363child template may override those portions of the template.364365Child Template366--------------
25 unmodified lines
392this template, first it locates the parent.393394The filename of the template depends on the template loader. For example the395``FileSystemLoader`` allows you to access other templates by giving the395filename. You can access subdirectory with an slash:396filename. You can access templates in subdirectories with an slash:397398.. sourcecode:: jinja399400 {% extends "layout/default.html" %}
14 unmodified lines
415416Template Inclusion417==================418418You can load another template at a given posiiton using ``{% include %}``.419You can load another template at a given position using ``{% include %}``.420Usually it's a better idea to use inheritance but if you for example want to420load macros ``include`` works better than ``extends``:421load macros, `include` works better than `extends`:422423.. sourcecode:: jinja424425 {% include "myhelpers.html" %}426 {{ my_helper("foo") }}427427If you define a macro called ``my_helper`` in ``myhelpers.html`` you can now428If you define a macro called ``my_helper`` in ``myhelpers.html``, you can now429use it from the template as shown above.430431Filtering Blocks432================433433Sometimes it could be a good idea to filter a complete block. For example if434you want to escape some html code:434Sometimes it could be a good idea to filter a complete block of text. For435example, if you want to escape some html code:436437.. sourcecode:: jinja438439 {% filter escape %}
1 unmodified line
441 <code>goes here</code>442 </html>443 {% endfilter %}444444Of course you can chain filters too.445Of course you can chain filters too:446447.. sourcecode:: jinja448449 {% filter lower|escape %}450 <B>SOME TEXT</B>451 {% endfilter %}452453returns ``"&lt;b&gt;some text&lt;/b&gt;"``.454455Defining Variables456==================457458You can also define variables in the namespace using the ``{% set %}`` tag:
2 unmodified lines
461462 {% set foo = 'foobar' %}463 {{ foo }}464456This should ouputput ``foobar``.465This should ouput ``foobar``.466467Scopes468======469
12 unmodified lines
482 Hello {{ name }}. Hello {{ name }}!483 HELLO {{ angryname }}!!!!!!111484 {% endmacro %}485477The variable ``angryname`` just exists inside of the macro, not outside of it.486The variable ``angryname`` just exists inside the macro, not outside it.487479Defined macros appear on the context as variables. Because of this those are488Defined macros appear on the context as variables. Because of this, they are489affected by the scoping too. A macro defined inside of a macro is just available490in those two macros (the macro itself and the macro it's defined in). For `set`482and `macro` two additional rules exist. If a macro is defined in an extended491and `macro` two additional rules exist: If a macro is defined in an extended492template but outside of a visible block (thus outside of any block) will be493available in all blocks below. This allows you to use `include` statements to494load often used macros at once.495496Internationalization497====================498490If the application is configured for i18n you can define translatable blocks499If the application is configured for i18n, you can define translatable blocks500for translators using the `trans` tag or the special underscore function:501502.. sourcecode:: jinja503
6 unmodified lines
510 {{ _("This is a translatable string") }}511512The latter one is useful if you want translatable arguments for filters etc.513505If you want to have plural forms too use the `pluralize` block:514If you want to have plural forms too, use the `pluralize` block:515516.. sourcecode:: jinja517518 {% trans users=users %}
7 unmodified lines
526 {% pluralize users %}527 {{ users }} users found, the first one is called {{ first }}.528 {% endtrans %}529521If you have multiple arguments the first one is assumed to be the indicator (the530If you have multiple arguments, the first one is assumed to be the indicator (the531number that is used to determine the correct singular or plural form. If you532don't have the indicator variable on position 1 you have to tell the `pluralize`533tag the correct variable name.534526Inside of translatable blocks you cannot use blocks or expressions. the variable527print syntax (``{{ variablename }}``) is just used to insert the variables defined535Inside translatable blocks you cannot use blocks or expressions. The variable536print syntax (``{{ variablename }}``) can just be used to insert the variables defined537in the ``trans`` header.538539.. admonition:: note540541 Please make sure that you always use pluralize blocks where required.533 Many languages have more complex plural forms than the english language.542 Many languages have more complex plural forms than the English language.543 544 Never try to workaround that issue by using something like this:545546 .. sourcecode:: jinja
6 unmodified lines