[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
Provenance
- Commit
22e1162- Tree
803ee5f- Author
Georg Brandl <georg@python.org>- Committer
Georg Brandl <georg@python.org>- Parent
4e0cd08
1 file changed+84 added−75 removed
modified:docs/src/designerdoc.txt+84 added-75 removed
44 unmodified lines45 the statement ``{% print foobar %}``.46 47 Variables are coming from the context provided by the application. Normally there48 should be a documentation regarding the context contents but if you want to know49-the content of the current context you can add this to your template:49+the content of the current context, you can add this to your template:50 51 .. sourcecode:: html+jinja52 53 <pre>{{ debug()|e }}</pre>54 55 A context isn't flat which means that each variable can has subvariables, as long56 as it is representable as python data structure. You can access attributes of57-a variable by using the dot and brace operators. The following examples show57+a variable using the dot and bracket operators. The following examples show58 this:59 60 .. sourcecode:: jinja61 8 unmodified lines70 Filters71 =======72 73 In the examples above you might have noticed the pipe symbols. Pipe symbols tell74-the engine that it has to apply a filter on the variable. Here a small example:74+the engine that it has to apply a filter on the variable. Here is a small example:75 76 .. sourcecode:: jinja77 78 {{ variable|replace('foo', 'bar')|escape }}79 80-If you like you can also put whitespace between the filters.80+If you want, you can also put whitespace between the filters.81 82-This will look for a variable variable, passes it to the filter replace with the83-arguments ``'foo'`` and ``'bar'``, and passes the result to the filter `escape`84-that automatically xml escapes the value. The ``e`` filter is an alias for85-``escape``. Here the complete list of supported filters:82+This will look for a variable `variable`, pass it to the filter `replace`83+with 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:86 87 [[list_of_filters]]88 89 .. admonition:: note90 91 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:94 95 .. sourcecode:: jinja96 9 unmodified lines106 107 Tests108 =====109 110-You can use the `is`-operator to perform tests on a value:110+You can use the `is` operator to perform tests on a value:111 112 .. sourcecode:: jinja113 114 {{ 42 is numeric }} -> true115 {{ "foobar" is numeric }} -> false116 {{ 'FOO' is upper }} -> true117 118-Those tests are especially useful if used in `if`-conditions.118+These tests are especially useful when used in `if` conditions.119 120 [[list_of_tests]]121 122 Loops123 =====124 125-To iterate over a sequence you can use the `for`-loop. If basically looks like a126-normal python for loop and works pretty much the same:125+To iterate over a sequence, you can use the `for` loop. It basically looks like a126+normal Python `for` loop and works pretty much the same:127 128 .. sourcecode:: html+jinja129 130 <h1>Members</h1>4 unmodified lines135 <li><em>no users found</em></li>136 {% endfor %}137 </ul>138 139-The optional ``else`` block is only executed if the template did not iterate140-because the sequence was empty.139+*Important difference from Python:* the optional ``else`` block is only140+executed if there was no iteration because the sequence was empty.141 142-Inside of a for loop block you can access some special variables:142+Inside of a `for` loop block you can access some special variables:143 144 +----------------------+----------------------------------------+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 +----------------------+----------------------------------------+170 171-Loops also support recursion. For example you have a sitemap where each item172-might have a number of child items. Such a template could look like this:171+Loops also support recursion. Let's assume you have a sitemap where each item172+might have a number of child items. A template for that could look like this:173 174 .. sourcecode:: html+jinja175 176 <h1>Sitemap3 unmodified lines180 {% if item.children %}<ul>{{ loop(item.children) }}</ul>{% endif %}</li>181 {% endfor %}182 </ul>183 184-Now. What happens here? Basically the first thing that is different to a normal184+What happens here? Basically the first thing that is different to a normal185 loop is the additional ``recursive`` modifier in the `for`-loop declaration.186 It tells the template engine that we want recursion. If recursion is enabled187-the special loop variable is callable. If you call it with a sequence it will188-automatically render that loop at that position with the new sequence as argument.187+the special `loop` variable is callable. If you call it with a sequence it will188+automatically render the loop at that position with the new sequence as argument.189 190 Cycling191 =======192 193-Sometimes you might want to have different classes for each row in a list. For194-example to have alternating row colors. You can easily do this by using the193+Sometimes you might want to have different text snippets for each row in a list,194+for example to have alternating row colors. You can easily do this by using the195 ``{% cycle %}`` tag:196 197 .. sourcecode:: html+jinja198 2 unmodified lines201 <li class="{% cycle 'row1', 'row2' %}">{{ message|e }}</li>202 {% endfor %}203 </ul>204 205-Each time Jinja encounters a cycle tag it will evaluate cycle through the list205+Each time Jinja encounters a `cycle` tag it will cycle through the list206 of given items and return the next one. If you pass it one item jinja assumes207 that this item is a sequence from the context and uses this:208 209 .. sourcecode:: html+jinja2 unmodified lines212 213 Conditions214 ==========215 216-Jinja supports python like ``if`` / ``elif`` / ``else`` constructs:216+Jinja supports Python-like `if` / `elif` / `else` constructs:217 218 .. sourcecode:: jinja219 220 {% if user.active %}18 unmodified lines239 {% endif %}240 241 .. admonition:: Note242 243- 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.248 249 Operators250 =========251 252-Inside ``{{ variable }}`` blocks, `if`-conditions and many other parts you can253-can use Expressions. In expressions you can use any of the following operators:252+Inside ``{{ variable }}`` blocks, `if` conditions and many other parts you can253+can use expressions. In expressions you can use any of the following operators:254 255 ======= ===================================================================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 ======= ===================================================================278 277-Note that there is no support for any bit operation or something similar.279+Note that there is no support for any bit operations or something similar.280 279-* 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)``.285 286 Macros287 ======288 287-If you want to use a partial template on more than one place you might want to288-create a macro out of it:289+If you want to use a partial template in more than one place, you might want to290+create a macro from it:291 292 .. sourcecode:: html+jinja293 294 {% macro show_user user %}28 unmodified lines323 ===========324 325 The most powerful part of Jinja is template inheritance. Template inheritance326 allows you to build a base "skeleton" template that contains all the common325-elements of your site and defines **blocks** that child326-templates can override.327+elements of your site and defines **blocks** that child templates can override.328 329 Sounds complicated but is very basic. It's easiest to understand it by starting330 with an example.331 26 unmodified lines358 </div>359 </body>360 361 In this example, the ``{% block %}`` tags define four blocks that child templates361-can fill in. All the ``block`` tag does is to tell the template engine that a362+can fill in. All the `block` tag does is to tell the template engine that a363 child template may override those portions of the template.364 365 Child Template366 --------------25 unmodified lines392 this template, first it locates the parent.393 394 The filename of the template depends on the template loader. For example the395 ``FileSystemLoader`` allows you to access other templates by giving the395-filename. You can access subdirectory with an slash:396+filename. You can access templates in subdirectories with an slash:397 398 .. sourcecode:: jinja399 400 {% extends "layout/default.html" %}14 unmodified lines415 416 Template Inclusion417 ==================418 418-You can load another template at a given posiiton using ``{% include %}``.419+You can load another template at a given position using ``{% include %}``.420 Usually it's a better idea to use inheritance but if you for example want to420-load macros ``include`` works better than ``extends``:421+load macros, `include` works better than `extends`:422 423 .. sourcecode:: jinja424 425 {% include "myhelpers.html" %}426 {{ my_helper("foo") }}427 427-If you define a macro called ``my_helper`` in ``myhelpers.html`` you can now428+If you define a macro called ``my_helper`` in ``myhelpers.html``, you can now429 use it from the template as shown above.430 431 Filtering Blocks432 ================433 433-Sometimes it could be a good idea to filter a complete block. For example if434-you want to escape some html code:434+Sometimes it could be a good idea to filter a complete block of text. For435+example, if you want to escape some html code:436 437 .. sourcecode:: jinja438 439 {% filter escape %}1 unmodified line441 <code>goes here</code>442 </html>443 {% endfilter %}444 444-Of course you can chain filters too.445+Of course you can chain filters too:446 447+.. sourcecode:: jinja448+449+ {% filter lower|escape %}450+ <B>SOME TEXT</B>451+ {% endfilter %}452+453+returns ``"<b>some text</b>"``.454+455 Defining Variables456 ==================457 458 You can also define variables in the namespace using the ``{% set %}`` tag:2 unmodified lines461 462 {% set foo = 'foobar' %}463 {{ foo }}464 456-This should ouputput ``foobar``.465+This should ouput ``foobar``.466 467 Scopes468 ======469 12 unmodified lines482 Hello {{ name }}. Hello {{ name }}!483 HELLO {{ angryname }}!!!!!!111484 {% endmacro %}485 477-The variable ``angryname`` just exists inside of the macro, not outside of it.486+The variable ``angryname`` just exists inside the macro, not outside it.487 479-Defined macros appear on the context as variables. Because of this those are488+Defined macros appear on the context as variables. Because of this, they are489 affected by the scoping too. A macro defined inside of a macro is just available490 in those two macros (the macro itself and the macro it's defined in). For `set`482-and `macro` two additional rules exist. If a macro is defined in an extended491+and `macro` two additional rules exist: If a macro is defined in an extended492 template but outside of a visible block (thus outside of any block) will be493 available in all blocks below. This allows you to use `include` statements to494 load often used macros at once.495 496 Internationalization497 ====================498 490-If the application is configured for i18n you can define translatable blocks499+If the application is configured for i18n, you can define translatable blocks500 for translators using the `trans` tag or the special underscore function:501 502 .. sourcecode:: jinja503 6 unmodified lines510 {{ _("This is a translatable string") }}511 512 The latter one is useful if you want translatable arguments for filters etc.513 505-If you want to have plural forms too use the `pluralize` block:514+If you want to have plural forms too, use the `pluralize` block:515 516 .. sourcecode:: jinja517 518 {% trans users=users %}7 unmodified lines526 {% pluralize users %}527 {{ users }} users found, the first one is called {{ first }}.528 {% endtrans %}529 521-If you have multiple arguments the first one is assumed to be the indicator (the530+If you have multiple arguments, the first one is assumed to be the indicator (the531 number that is used to determine the correct singular or plural form. If you532 don't have the indicator variable on position 1 you have to tell the `pluralize`533 tag the correct variable name.534 526-Inside of translatable blocks you cannot use blocks or expressions. the variable527-print syntax (``{{ variablename }}``) is just used to insert the variables defined535+Inside translatable blocks you cannot use blocks or expressions. The variable536+print syntax (``{{ variablename }}``) can just be used to insert the variables defined537 in the ``trans`` header.538 539 .. admonition:: note540 541 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:545 546 .. sourcecode:: jinja6 unmodified lines