Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 36 lines (22 sloc) 1.072 kb
c58f443 @kaos doc: migrate the remaining tag docs.
kaos authored
1
2 .. index:: tag; with
3 .. _tag-with:
4
5 with
6 ====
7
8 Assign a complex value to a variable.
9
10 The ``{% with %}`` tag assigns the result of a variable expression to a new variable. This is useful when accessing an “expensive” method (e.g., one that hits the database) multiple times. The ``{% with %}`` tag is often used in conjunction with the search model :ref:`m.search <model-search>`.
11
12 For example::
13
14 {% with m.search[{latest cat="news"}] as latest_news %}
15 The latest {% latest_news|length %} news articles:
16 {% for id in latest_news %}
17 {{ m.rsc[id].title }}
18 {% endfor %}
19 {% endwith %}
20
21 This outputs the number of latest news articles and also the titles of the news articles. The search is only done once when the ``{% with %}`` tag assigns the variable “latest_news”.
22
6e5cefb @arjan doc: Add paragraph about multiple assignments to `with` tag docs
arjan authored
23
24 Multiple assignments
25 --------------------
26
27 It is also possible to assign multiple variables with a single `with` statement, like this::
28
29 {% with "value1", "value2" as foo, bar %}
30 {{ foo }}
31 {{ bar }}
32 {% endwith %}
33
34 This will output ``value1 value2``.
35
Something went wrong with that request. Please try again.