flask_六、全局函数+过滤器+测试器+修改定界值
一、简单函数说明

1.1、app.py文件内容
#encoding=utf-8 from flask import Flask,render_template,render_template_string app = Flask(__name__) @app.route('/index/') def index(): return render_template('index.html') @app.route('/watchlist') def watchlist(): return render_template_string('watchlist.html', movies = movies) #return render_template('watchlist.html',user=user,movies = movies) @app.route('/hello') def hello(): return "hello flask" @app.template_global(name='barfunc') def bar(): return 'I am bar.' @app.context_processor def inject_foo(): return dict(my_func=my_func) def my_func(): return "this is my_func~" if __name__ == '__main__': app.run(debug=True)
1.2index.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- {% set name="gloryroad" %}--> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <hr /> {% for i in range(10) %} {{ i }} {% endfor %} <hr /> <!--随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签--> {{ lipsum(n=2, html=True, min=10, max=20) }} <hr /> {{ dict(a=1,b=2) }} </body> </html>
1.3 运行结果

二:将函数注册为模板全局函数
扩展:url_for也是全局函数
2.1 app.py
#将函数注册为模板全局函数
# encoding=utf-8 from flask import Flask,render_template,render_template_string app = Flask(__name__) @app.route('/index/') def index(): return render_template('index.html') @app.route('/watchlist') def watchlist(): return render_template_string('watchlist.html', movies = movies) #return render_template('watchlist.html',user=user,movies = movies) @app.route('/hello') def hello(): return "hello flask" @app.template_global() def bar(): return 'I am bar.' @app.context_processor def inject_foo(): return dict(my_func=my_func) def my_func(): return "this is my_func~" if __name__ == '__main__': app.run(debug=True)
2.2 index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- {% set name="gloryroad" %}--> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <hr /> <p>调用bar()的结果: {{ bar() }}</p> </body> </html>
2.3运行结果:

三、另外一种形式传全局函数——app.add_template_global(bar, name='barfunc')
app.py内容
#encoding=utf-8 from flask import Flask,render_template,render_template_string app = Flask(__name__) # @app.template_global() def bar(): return 'I am bar ' app.add_template_global(bar, name='barfunc') @app.route('/index/') def index(): return render_template('index.html') @app.route('/watchlist') def watchlist(): return render_template_string('watchlist.html', movies = movies) #return render_template('watchlist.html',user=user,movies = movies) @app.route('/hello') def hello(): return "hello flask" @app.context_processor def inject_foo(): return dict(my_func=my_func) def my_func(): return "this is my_func~" if __name__ == '__main__': app.run(debug=True)
index.html内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- {% set name="gloryroad" %}--> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <hr /> <p>调用barfunc()的结果: {{ barfunc() }}</p> </body> </html>
运行结果:

四:过滤器
在jinja2中,过滤器(filter)是一些可以用来修改和过滤变量值的特殊函数,过滤器和变量用一个竖线(管道符号)隔开,需要参数的过滤器可以像函数一样使用括号传递。
下面是一个对name变量使用title过滤器的例子:
{{ name|title }}

4.1 index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- {% set name="gloryroad" %}--> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <!--<hr />--> {% set name="gloryroad" %} <p>hello {{ name }}</p> <!--<首字母大写>--> <p>hello {{ name|title }}</p> <p>hello {{ name|upper }}</p> </body> </html>
4.2 app.py文件
#encoding=utf-8 from flask import Flask,render_template,render_template_string app = Flask(__name__) # @app.template_global() # def bar(): # return 'I am bar %s' # app.add_template_global(bar, name='barfunc') @app.route('/index/') def index(): return render_template('index.html') @app.route('/watchlist') def watchlist(): return render_template_string('watchlist.html', movies = movies) #return render_template('watchlist.html',user=user,movies = movies) @app.route('/hello') def hello(): return "hello flask" @app.context_processor def inject_foo(): return dict(my_func=my_func) def my_func(): return "this is my_func~" if __name__ == '__main__': app.run(debug=True)
4.3 运行结果

五、自定义过滤器的效果
app.py文件没变
5.1Html文件内容如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- {% set name="gloryroad" %}--> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <!--<hr />--> <!--{% set name="gloryroad" %}--> <!--<p>hello {{ name }}</p>--> <!--<p>hello {{ name|title }}</p>--> <!--<p>hello {{ name|upper }}</p>--> <h1>Hello {{ name|d("陌生人")|title }}</h1> <h1>max of [1,2,3,4] {{ [1,2,3,4]|max() }}</h1> <h1>first of [1,2,4] {{ [1,2,3]|first() }}</h1> </body> </html>
5.2运行结果

六、取消默认值的效果
app.py文件内容不变,只更改index.html
注意:仅注释可能不生效,需要在index.html文件中,删掉如下内容
<!-- {% set name="gloryroad" %}-->
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <!--<hr />--> <h1>Hello {{ name|d("陌生人")|title }}</h1> <h1>max of [1,2,3,4] {{ [1,2,3,4]|max() }}</h1> <h1>first of [1,2,4] {{ [1,2,3]|first() }}</h1> </body> </html>
d的意思是:若没有name则取默认值“陌生人”
运行效果

七、自定义过滤器——app.template_filter()
因为音符通过HTML实体♫表示,我们使用Markup类将它标记为安全字符
注意:Markup不支持int方式,要为字符串,否则会报错,需要加个str()转一下
7.1、app.py
# #encoding=utf-8 from flask import Flask,render_template,render_template_string app = Flask(__name__) # @app.template_global() # def bar(): # return 'I am bar %s' # app.add_template_global(bar, name='barfunc') @app.route('/index/') def index(): return render_template('index.html') @app.route('/watchlist') def watchlist(): return render_template_string('watchlist.html', movies = movies) #return render_template('watchlist.html',user=user,movies = movies) @app.route('/hello') def hello(): return "hello flask" @app.context_processor def inject_foo(): return dict(my_func=my_func) def my_func(): return "this is my_func~" from flask import Markup @app.template_filter() def musical(s): return str(s) + Markup(' ♫') if __name__ == '__main__': app.run(debug=True)
7.2 index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <!--<hr />--> <!--<h1>Hello {{ name|d("陌生人")|title }}</h1>--> <!--<h1>max of [1,2,3,4] {{ [1,2,3,4]|max() }}</h1>--> <!--<h1>first of [1,2,4] {{ [1,2,3]|first() }}</h1>--> <hr /> {% set name='beautiful' %} <h1>Hello, {{ name|length|musical }}!</h1> </body> </html>
7.3 运行结果

八、实例二
8.1 app.py文件
# encoding=utf-8 from flask import Flask,render_template, render_template_string from flask import Markup app = Flask(__name__) # @app.template_filter(name='filterMusical') def musical(s): return str(s) + Markup(' ♫') app.add_template_filter(musical,name='filterMusical') # @app.template_global() def bar(): return 'I am bar.' app.add_template_global(bar, name='barfunc') @app.context_processor def inject_foo(): return dict(my_func=my_func) def my_func(): return "this is my_func~" @app.template_global(name='barfunc') def bar(): return 'I am bar.' @app.route('/index/') def index(): return render_template('index.html') @app.context_processor def inject_foo(): foo = 'I am foo.' return dict(foo=foo)#等同于return {'foo': foo} @app.route('/watchlist') def watchlist(): return render_template_string('watchlist.html', movies = movies) #return render_template('watchlist.html',user=user,movies = movies) @app.route('/hello') def hello(): return "hello flask" @app.route('/testJinja') def testJinja(): return render_template('testJinja.html', my_list=my_list, my_tuple=my_tuple, my_dict=my_dict, my_func=my_func, my_object=my_object) my_list=[1,2,3,4] my_tuple=('a','b','c','d') my_dict={'d1':'abc','d2':'bcd','d3':'cde'} def my_func(): return "this is my_func~" my_object = "my_object" if __name__ == '__main__': app.run(debug=True)
8.2 index.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <!--<hr />--> <!--<h1>Hello {{ name|d("陌生人")|title }}</h1>--> <!--<h1>max of [1,2,3,4] {{ [1,2,3,4]|max() }}</h1>--> <!--<h1>first of [1,2,4] {{ [1,2,3]|first() }}</h1>--> <!--<hr />--> {% set name='beautiful' %} <h1>Hello, {{ name|title|upper|length|filterMusical }}!</h1> </body> </html>
8.3运行结果

九、测试器
在jinja2中,测试器(Test)是一些用来测试变量或表达式,返回布尔值(True或False)的特殊函数。比如,number测试器用来判断一个变量或表达式是否是数字,我们使用is连接变量和测试器:
app.py文件内容不变
仅仅会有index.html文件的更新
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <!--<hr />--> <!--<h1>Hello {{ name|d("陌生人")|title }}</h1>--> <!--<h1>max of [1,2,3,4] {{ [1,2,3,4]|max() }}</h1>--> <!--<h1>first of [1,2,4] {{ [1,2,3]|first() }}</h1>--> <!--<hr />--> {% set name='beautiful' %} <h1>Hello, {{ name|title|upper|length|filterMusical }}!</h1> <hr /> {% set age="123" %} {% if age is number %} age*365等于: {{ age * 365 }} {% else %} 无效的数字 {% endif %} </body> </html>
运行结果:

Index.html文件内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<!-- <p>hello {{ name }}</p>-->
<!-- {% set navigation %}-->
<!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>-->
<!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>-->
<!-- {% endset %}-->
<!-- {{ navigation }}-->
<!-- <hr />-->
<!-- <p>config: {{ config }}</p>-->
<!-- <p>config.DEBUG: {{ config.DEBUG }}</p>-->
<!-- <p>request: {{ request }}</p>-->
<!-- <p>request.method: {{ request.method }}</p>-->
<!-- <p>session: {{ session }}</p>-->
<!-- <p>g: {{ g }}</p>-->
<!--<hr />-->
<!--<p>my_func(): {{ my_func() }}</p>-->
<!--<hr />-->
<!--{% for i in range(10) %}-->
<!--{{ i }}-->
<!--{% endfor %}-->
<!--<hr />-->
<!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>-->
<!--{{ lipsum(n=2, html=True, min=10, max=20) }}-->
<!--<hr />-->
<!--{{ dict(a=1,b=2) }}-->
<!--<hr />-->
<!--<h1>Hello {{ name|d("陌生人")|title }}</h1>-->
<!--<h1>max of [1,2,3,4] {{ [1,2,3,4]|max() }}</h1>-->
<!--<h1>first of [1,2,4] {{ [1,2,3]|first() }}</h1>-->
<!--<hr />-->
<!--{% set name='beautiful' %}-->
<!--<h1>Hello, {{ name|title|upper|length|filterMusical }}!</h1>-->
<!--<hr />-->
<hr />
{% set age="123" %}
{% set foo="123" %}
{% if age is sameas(foo) %}
age等于foo
{% else %}
age不等于foo
{% endif %}
</body>
</html>
运行效果:

十、自定义测试器
10.1、app.py文件内容
# encoding=utf-8 from flask import Flask,render_template, render_template_string from flask import Markup app = Flask(__name__) #@app.template_filter(name='filterMusical') def musical(s): return str(s) + Markup(' ♫') app.add_template_filter(musical,name='filterMusical') # @app.template_global() def bar(): return 'I am bar.' app.add_template_global(bar, name='barfunc') @app.context_processor def inject_foo(): return dict(my_func=my_func) def my_func(): return "this is my_func~" @app.template_global(name='barfunc') def bar(): return 'I am bar.' @app.route('/index/') def index(): return render_template('index.html') @app.context_processor def inject_foo(): foo = 'I am foo.' return dict(foo=foo)#等同于return {'foo': foo} @app.route('/watchlist') def watchlist(): return render_template_string('watchlist.html', movies = movies) #return render_template('watchlist.html',user=user,movies = movies) @app.route('/hello') def hello(): return "hello flask" @app.route('/testJinja') def testJinja(): return render_template('testJinja.html', my_list=my_list, my_tuple=my_tuple, my_dict=my_dict, my_func=my_func, my_object=my_object) my_list=[1,2,3,4] my_tuple=('a','b','c','d') my_dict={'d1':'abc','d2':'bcd','d3':'cde'} def my_func(): return "this is my_func~" my_object = "my_object" @app.template_test() def baz(n): if n == "baz": return True return False if __name__ == '__main__': app.run(debug=True)
Index.html文件内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <!--<hr />--> <!--<h1>Hello {{ name|d("陌生人")|title }}</h1>--> <!--<h1>max of [1,2,3,4] {{ [1,2,3,4]|max() }}</h1>--> <!--<h1>first of [1,2,4] {{ [1,2,3]|first() }}</h1>--> <!--<hr />--> <!--{% set name='beautiful' %}--> <!--<h1>Hello, {{ name|title|upper|length|filterMusical }}!</h1>--> <!--<hr />--> <!--<hr />--> <!--{% set age="123" %}--> <!--{% set foo="123" %}--> <!--{% if age is sameas(foo) %}--> <!--age等于foo--> <!--{% else %}--> <!--age不等于foo--> <!--{% endif %}--> <hr /> {% set foo="baz" %} {% if foo is baz() %} foo的值是"baz" {% else %} foo的值不是"baz" {% endif %} </body> </html>
运行结果:

十一、修改定界符的起始和标志符
# encoding=utf-8 from flask import Flask,render_template, render_template_string from flask import Markup app = Flask(__name__) app.jinja_env.variable_start_string = '[[' app.jinja_env.variable_end_string = ']]' #@app.template_filter(name='filterMusical') def musical(s): return str(s) + Markup(' ♫') app.add_template_filter(musical,name='filterMusical') # @app.template_global() def bar(): return 'I am bar.' app.add_template_global(bar, name='barfunc') @app.context_processor def inject_foo(): return dict(my_func=my_func) def my_func(): return "this is my_func~" @app.template_global(name='barfunc') def bar(): return 'I am bar.' @app.route('/index/') def index(): return render_template('index.html') @app.context_processor def inject_foo(): foo = 'I am foo.' return dict(foo=foo)#等同于return {'foo': foo} @app.route('/watchlist') def watchlist(): return render_template_string('watchlist.html', movies = movies) #return render_template('watchlist.html',user=user,movies = movies) @app.route('/hello') def hello(): return "hello flask" @app.route('/testJinja') def testJinja(): return render_template('testJinja.html', my_list=my_list, my_tuple=my_tuple, my_dict=my_dict, my_func=my_func, my_object=my_object) my_list=[1,2,3,4] my_tuple=('a','b','c','d') my_dict={'d1':'abc','d2':'bcd','d3':'cde'} def my_func(): return "this is my_func~" my_object = "my_object" @app.template_test() def baz(n): if n == "baz": return True return False if __name__ == '__main__': print(dir(app.jinja_env.globals)) print(app.jinja_env.globals) print(app.jinja_env.filters) print(app.jinja_env.tests) app.run(debug = True)
Index.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <!-- <p>hello {{ name }}</p>--> <!-- {% set navigation %}--> <!-- <li><a href="{{ url_for('watchlist') }}">Watchlist</a></li>--> <!-- <li><a href="{{ url_for('hello') }}">Hello</a></li>--> <!-- {% endset %}--> <!-- {{ navigation }}--> <!-- <hr />--> <!-- <p>config: {{ config }}</p>--> <!-- <p>config.DEBUG: {{ config.DEBUG }}</p>--> <!-- <p>request: {{ request }}</p>--> <!-- <p>request.method: {{ request.method }}</p>--> <!-- <p>session: {{ session }}</p>--> <!-- <p>g: {{ g }}</p>--> <!--<hr />--> <!--<p>my_func(): {{ my_func() }}</p>--> <!--<hr />--> <!--{% for i in range(10) %}--> <!--{{ i }}--> <!--{% endfor %}--> <!--<hr />--> <!--<!–随机函数,最小是10个单词数,最大是20单词数,n是2行即为2段,flase的话没有p标签–>--> <!--{{ lipsum(n=2, html=True, min=10, max=20) }}--> <!--<hr />--> <!--{{ dict(a=1,b=2) }}--> <!--<hr />--> <!--<h1>Hello {{ name|d("陌生人")|title }}</h1>--> <!--<h1>max of [1,2,3,4] {{ [1,2,3,4]|max() }}</h1>--> <!--<h1>first of [1,2,4] {{ [1,2,3]|first() }}</h1>--> <!--<hr />--> <!--{% set name='beautiful' %}--> <!--<h1>Hello, {{ name|title|upper|length|filterMusical }}!</h1>--> <!--<hr />--> <!--<hr />--> <!--{% set age="123" %}--> <!--{% set foo="123" %}--> <!--{% if age is sameas(foo) %}--> <!--age等于foo--> <!--{% else %}--> <!--age不等于foo--> <!--{% endif %}--> <!--<hr />--> <!--{% set foo="baz" %}--> <!--{% if foo is baz() %}--> <!--foo的值是"baz"--> <!--{% else %}--> <!--foo的值不是"baz"--> <!--{% endif %}--> <hr/> {% set foo = "baz" %} [[ foo ]] </body> </html>
运行效果

效果:

说明:会出现只打印一次的效果

浙公网安备 33010602011771号