Flask在模板中有常用的几种控制语句:
-
if控制语句 -
for控制语句
下面来看看示例加强理解,如下:
模板中的if控制语句
1. 示例视图函数
@app.route('/user')
def user():
user = 'libai'
return render_template('user.html',user=user)
2.示例模板
<html>
<head>
{% if user %}
<title> hello {{user}} </title>
{% else %}
<title> welcome to flask </title>
{% endif %}
</head>
<body>
<h1>hello world</h1>
</body>
</html>
模板中的for循环语句
1. 示例视图函数
@app.route('/loop')
def loop():
fruit = ['apple','orange','pear','grape']
return render_template('loop.html',fruit=fruit)
2.示例模板
<html>
<head>
{% if user %}
<title> hello {{user}} </title>
{% else %}
<title> welcome to flask </title>
{% endif %}
</head>
<body>
<h1>hello world</h1>
<ul>
{% for item in fruit %}
<li>{{ item }}</li>
{% endfor %}
</ul>
</body>
</html>
原文始发于微信公众号(海洋的渔夫):20. Flask 模板控制语句 if for
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/30950.html