标签:utf-8 port char inpu set 传递 safe type 定义函数
引子:
flask模板语言通过插件 jinja2 来支持,使用方法和django基本一致
代码:
python代码
# -*- coding: utf-8 -*-
from flask import Flask,render_template,Markup
app=Flask(__name__)
"""
传递函数
"""
def fun1(arg):
return arg
def fun2(args):
return ‘<input type="text" value="%s">‘%(args)
def fun3(args):
"""
后端允许转义
:param args:
:return:
"""
return Markup(‘<input type="text" value="%s">‘%(args))
@app.route(‘/‘)
def index():
return render_template("temple_demo.html",ff=fun1)
@app.route(‘/home‘)
def home():
"""
发送字符串类型的 html 标签
:return:
"""
return render_template("homd_demo.html",kk=fun2)
@app.route("/mark")
def markup_demo():
"""
后台 转义
:return:
"""
return render_template("mark_demo.html",xxx=fun3)
@app.route(‘/macro‘)
def macro_demo():
"""
宏定义
:return:
"""
return render_template("macro_demo.html")
if __name__ == ‘__main__‘:
app.run()
temple_demo.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<h1>jinja2模板语言 类似django 但是个人感觉比django 的更好一点 更加亲近python的原生语法</h1>
<h1>{{ ff(nimei) }} </h1>
<script>
</script>
</body>
</html>
homd_demo.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<h1> {{ kk("nimei")|safe }}======safe 的html 代码</h1>
<h1>和django 一样 通过safe 允许转义</h1>
<h1>{{ kk("aoteman") }}=======没有safe的</h1>
<script>
</script>
</body>
</html>
mark_demo.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
后台转义 Markup
{{ xxx("蓝色的大螃蟹") }}
<script>
</script>
</body>
</html>
macro_demo.html 宏定义 在django 中没有 jinja2支持的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<h1>宏定义 django中没有 </h1>
<h1>使用关键字 macro 定义函数 然后下方调用 当成普通函数既可以</h1>
{% macro func(name,type=‘text‘,vlaue=‘‘) %}
<input type="{{ type }}" name="{{ name }}1" value="{{ vlaue }}" >
<input type="{{ type }}" name="{{ name }}11" value="{{ vlaue }}" >
<input type="{{ type }}" name="{{ name }}111" value="{{ vlaue }}" >
<input type="{{ type }}" name="{{ name }}1111" value="{{ vlaue }}" >
{% endmacro %}
{{ func("n") }}
<script>
</script>
</body>
</html>
标签:utf-8 port char inpu set 传递 safe type 定义函数
原文地址:https://www.cnblogs.com/yuan-x/p/14359459.html