Twig 的 tags学习(中文) 之一(2)
</ul>
<ul>
{% for user in users if user.active %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
else 分支
如果 users是个空数组就会输出no user found 。
<ul>
{% for user in users %}
<li>{{ user.username}}</li>
{% else %}
<li><em>no user found</em></li>
{% endfor %}
</ul>
<ul>
{% for user in users %}
<li>{{ user.username}}</li>
{% else %}
<li><em>no user found</em></li>
{% endfor %}
</ul>
按keys循环
<h1>Members</h1>
<ul>
{% for key in users|keys %}
<li>{{ key }}</li>
{% endfor %}
</ul>
<h1>Members</h1>
<ul>
{% for key in users|keys %}
<li>{{ key }}</li>
{% endfor %}
</ul>
按keys, values循环
<h1>Members</h1>
<ul>
{% for key, user in users %}
<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}
</ul>
<h1>Members</h1>
<ul>
{% for key, user in users %}
<li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}
</ul>
if标签
这个不用多说,直接看例子{% if users %}
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
{% endif %}
{% if kenny.sick %}
Kenny is sick.
{% elseif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
{% if users %}
<ul>
{% for user in users %}
相关新闻>>
- 发表评论
-
- 最新评论 更多>>