if控制语句
if、elif、else、endif
for循环
for、else、endfor
loop.index 下标从1开始
loop.index0 下标从0开始
loop.first 判断是否是第一个值,返回False、True
loop.last 判断是否是最后一个值,返回False、True
loop.length 返回loop的长度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>商品列表</title>
<style>
.red{
background: red;
}
.blue{
background: blue;
}
</style>
</head>
<body>
<div>
<ul>
{% for p in fruit %}
{% if loop.first %}
<li>{{ p }}</li>
{% elif loop.last %}
<li>{{ p }}</li>
{% else %}
<li>{{ p }}</li>
{% endif %}
{% endfor %}
</ul>
<table>
<thead>
<tr>
<td>编号</td>
<td>书名</td>
<td>作者</td>
<td>价格</td>
</tr>
</thead>
<tbody>
{% for book in books %}
{% if loop.first %}
<tr class="red">
{% elif loop.last %}
<tr class="blue">
{% else %}
<tr>
{% endif %}
<td>{{ loop.index }}</td>
<td>{{ book.name }}</td>
<td>{{ book.author }}</td>
<td>{{ book.price }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
from flask import Flask, render_template
@app.route('/product_list/')
def product_list():
p_list = {
"fruit": ["苹果", '香蕉', '梨子'],
"books": [
{
"name": "西游记",
"author": "吴承恩",
"price": 100,
},
{
"name": "红楼梦",
"author": "曹雪芹",
"price": 102,
},
{
"name": "水浒传",
"author": "施耐庵",
"price": 99,
},
{
"name": "狂人日记",
"author": "鲁迅",
"price": 33,
}
]
}
return render_template('product_list.html', **p_list)