table of Contents
First, the template syntax
Rendering variables: {{}} variable name
Cycle: {% for index, items in data.items ()%}
Analyzing conditions: {IF variable name%%} {% else%} { % endif%}
Executive function: {{function name ( "arguments")}}
Implementation of HTML: {{variable name | safe}}
from flask import Flask,render_template,Markup
app = Flask(__name__)
USERS = {
1:{'name':'张三','age':18,'gender':'男','text':"道路千万条"},
2:{'name':'李四','age':28,'gender':'男','text':"安全第一条"},
3:{'name':'王五','age':18,'gender':'女','text':"行车不规范"},
}
def func1(flag):
return Markup(f"<h1>jason dsb ?{flag}</h1>" )
@app.route("/index")
def index():
data=USERS
flag=True
htm="<h1>jason dsb</h1>"
return render_template("index1.html",data=data,flag=flag,name="sb",name1="sb1",htm=htm,func=func1)
if __name__ == '__main__':
app.run()
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for index,items in data.items() %}
<tr>
<td>{{index}}</td>
<td>{{items}}</td>
<td>{{items.name}}</td>
<td>{{items['name']}}</td>
<td>{{items.get('name')}}</td>
<td><a href="/detail/{{index}}">查看详细</a></td>
</tr>
{% endfor %}
{% if flag %}
{{name}}
{% else %}
{{name1}}
{% endif %}
<br>
{{htm|safe}}
{{func("yes")}}
</body>
</html>
note:
1.Markup django equivalent of mark_safe,
2.extends, include exactly the same