flaskjinjia2模板

模板

'''
flask模板默认使用Jinja2 模板引擎库,
也可模板继承,
include组件的引入
自定义标签的使用
和其他模版的扩展
自由,灵活,扩展性强
'''

view.py

flask比django更加接近Python。

from flask import Flask,render_template

app = Flask(name,)

def func(arg):
return '你好' + arg

@app.route('/md')
def index():
nums = [11,222,33]
return render_template('index.html',nums=nums,f=func)

if name == 'main':
app.run()

base.html





Title



{% block content %} {% endblock %}



include.html







extends+include

{% extends 'layout.html' %}

{% block content %}

MD


{% include 'form.html' %}
{{ f("david") }}

定义全局模板方法

'''

全局变量:@app.template_global()

过滤器:@app.template_filter()

'''

from flask import Flask,render_template

app = Flask(name,)

@app.template_global() # {{ func("david") }}
def func(arg):
return 'david' + arg

@app.template_filter() # {{ "hello"|x1("world") }}
def x1(arg,name):
return 'hello' + arg + name

@app.route('/md/hg')
def index():
return render_template('md_hg.html')

if name == 'main':
app.run()

注意:在蓝图中注册时候,应用返回只有本蓝图。

posted @ 2019-11-22 18:55  阿浪阿浪  阅读(187)  评论(0)    收藏  举报