模板

模板

from flask import Flask,render_template,redirect,request,jsonify,make_response,Markup
app = Flask(__name__)

@app.template_global()
def sbbbbbbb(a1, a2):
    """
    每个模板中可以调用的函数
    :param a1:
    :param a2:
    :return:
    """
    return a1 + a2


def gen_input(value):
    # return "<input value='%s'/>" %value
    return Markup("<input value='%s'/>" %value)

@app.route('/x1',methods=['GET','POST'])
def index():
    context = {
        'k1':123,
        'k2': [11,22,33],
        'k3':{'name':'oldboy','age':84},
        'k4': lambda x: x+1,
        'k5': gen_input, # 当前模板才能调用的函数
    }

    return render_template('index.html',**context)


@app.route('/x2',methods=['GET','POST'])
def order():
    context = {
        'k1':123,
        'k2': [11,22,33],
    }

    return render_template('order.html',**context)




if __name__ == '__main__':
    app.run()

index.html

{% extends 'layout.html'%}

{% block content %}
    <h1>{{k1}}</h1>
    <h1>{{k2.0}}  {{k2[0]}} </h1>
    <h1>{{k3.name}}  {{k3['name']}}  {{k3.get('name',888)}}</h1>
    <h1>{{k4(66)}}</h1>
    <h1>{{k5(99)}}</h1>
    <h1>{{sbbbbbbb(1,2)}}</h1>
{% endblock%}

order.html

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
    <h1>{{sbbbbbbb(1,2)}}</h1>
</body>
</html>

layout.html

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
</head>
<body>
    <div >头部</div>
    <div>
        {% block content %} {% endblock %}
    </div>
    <div >底部</div>
</body>
</html>
posted @ 2021-06-07 16:42  wuyuan2011woaini  阅读(43)  评论(0编辑  收藏  举报