flask的模板渲染
from flask import Flask, render_template, Markup# markup相当于django的reverse
app = Flask(__name__)
app.debug = True
USERS = {
1:{'name':'张三','age':18,'gender':'男','text':"道路千万条"},
2:{'name':'李四','age':28,'gender':'男','text':"安全第一条"},
3:{'name':'王五','age':18,'gender':'女','text':"行车不规范"},
}
def func1(arg, tank):
# Markup可以支持前端的代码在后端书写
return Markup(f"<h1>alen真的好帅啊, {arg} is sb {tank} is same as {arg}</h1>")
@app.route('/')
def index():
# data = {
# "user" :USERS,
# "name": "jason"
# }
return render_template('index.html', user=USERS, name="jason",ht1=func1, ht="<h1>alen真帅啊</h1>")
# return render_template('index.html', **data)
if __name__ == '__main__':
app.run()
html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> 我是html</h1>
<table>
{% for k,v in user.items() %}
<tr>
<td>{{ k }}</td>
<td>{{ v.name }}</td>
<td>{{ v['name'] }}</td>
<td>{{ v.get('name') }}</td>
<td>{{url_for("index")}}</td>
</tr>
{% endfor %}
</table>
<div>{{name}}</div>
{% if name == "jason" %}
<h1>is sb</h1>
{% else %}
<h1>水哥</h1>
{% endif %}
{{ ht|safe}}
{{ht1("jaosn","tank")}}
</body>
</html>