## **迭代列表**
~~~
{% for vo in users %}
<li>{{ vo.username | title }}</li>
{% endfor %}
~~~
<br/>
## **迭代字典**
~~~
<dl>
{% for key, value in my_dict.iteritems() %}
<dt>{{ key }}</dt>
<dd>{{ value}}</dd>
{% endfor %}
</dl>
~~~
<br/>
## **帶條件循環**
~~~
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no users found</em></li>
{% endfor %}
</ul>
~~~
<br/>
## **遞歸地使用循環**
>[info] 你處理諸如站點地圖之類的遞歸數據時很有用。要遞歸地 使用循環,你只需要在循環定義中加上?recursive?修飾,并在你想使用遞歸的地 方,對可迭代量調用?loop?變量。
下面的例子用遞歸循環實現了站點地圖:
~~~
<ul class="sitemap">
{%- for item in sitemap recursive %}
<li><a href="{{ item.href|e }}">{{ item.title }}</a>
{%- if item.children -%}
<ul class="submenu">{{ loop(item.children) }}</ul>
{%- endif %}</li>
{%- endfor %}
</ul>
~~~