基本來自《flask web開發(fā)》罢艾。
1.判斷結(jié)構(gòu) if...else
- 模板中(user.html):
{% if name %}
Hello, {{name}}!
{% else %}
Hello, stranger!
{% endif %}
- 視圖函數(shù)中:
return render_template('user.html', name = value)
2.循環(huán)結(jié)構(gòu) for
- 模板(comment.html):
{% for comment in comments %}
<li>
{{comment}}
</li>
{% endfor %}
*視圖函數(shù)中:
@app.route('/comment/<number>')
def comment(number):
comment_list = range(number)
return render_template('comment.html', comments = comment_list)
3.宏 macro
它就是函數(shù),只是寫法有點不同
{% macro funciton(arg) %}
pass
{% endmacro %}
等價于:
def function(arg):
pass
- 它可以單獨放在一個文件中溉卓,然后引用:
{% import 'macros.html' as func %}
- 而模板的引入:
{% include 'base.html' %}
「這倆的差異主要在于昔榴,import是引入的宏,或者說是代碼〖迹」
「而include則是引入的文件「幔」
- 模板繼承
{% exdents '<path>' %}
<path>就是要繼承的base.html的路徑吼鳞。本地一般用相對,網(wǎng)絡(luò)用絕對叫搁。