From: Fabien Potencier Date: Sun, 20 Nov 2011 09:26:23 +0000 (+0100) Subject: adding more structure to the for doc X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=c3eceb48265addc2e5a8ef127826d226b7fe26b0;p=web%2Fkonrad%2Ftwig.git adding more structure to the for doc --- diff --git a/doc/tags/for.rst b/doc/tags/for.rst index 6eed3b4..57cbddb 100644 --- a/doc/tags/for.rst +++ b/doc/tags/for.rst @@ -50,6 +50,9 @@ The ``..`` operator can take any expression at both sides: If you need a step different from 1, you can use the ``range`` function instead. +The `loop` variable +------------------- + Inside of a ``for`` loop block you can access some special variables: ===================== ============================================================= @@ -74,6 +77,9 @@ Variable Description .. versionadded:: 1.2 The ``if`` modifier support has been added in Twig 1.2. +Adding a condition +------------------ + Unlike in PHP, it's not possible to ``break`` or ``continue`` in a loop. You can however filter the sequence during iteration which allows you to skip items. The following example skips all the users which are not active: @@ -97,6 +103,9 @@ counting the users not iterated over. incremented when the condition is true (so the condition will never match). +The `else` Clause +----------------- + If no iteration took place because the sequence was empty, you can render a replacement block by using ``else``: @@ -110,6 +119,9 @@ replacement block by using ``else``: {% endfor %} +Iterating over Keys +------------------- + By default, a loop iterates over the values of the sequence. You can iterate on keys by using the ``keys`` filter: @@ -122,6 +134,9 @@ on keys by using the ``keys`` filter: {% endfor %} +Iterating over Keys and Values +------------------------------ + You can also access both keys and values: .. code-block:: jinja