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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>头</h1>
        {% block content %} {% endblock %}
    <h1>底</h1>
</body>
</html>

include.html

<form action="">
    <input type="text">
    <input type="text">
    <input type="text">
    <input type="text">
    <input type="text">
</form>

extends+include

{% extends 'layout.html' %}


{% block content %}
    <h1>MD</h1>
    {% include 'form.html' %}
    {{ f("david") }}
{% endblock %}

定义全局模板方法

'''

全局变量:@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()

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

猜你喜欢

转载自www.cnblogs.com/daviddd/p/11913444.html