模板宏的使用

一.模板宏的使用

  macro_demo.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#author tom


from flask import Flask,render_template


app = Flask(__name__)

@app.route("/")
def func():
    return render_template("macro.html")


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

  macro.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板宏的使用</title>
</head>
<body>
    //不带参数的宏
    {% macro input() %}
        <input type="text" name="" id="" size="30">
    {% endmacro %}

    <h1>input</h1>
    {{ input() }}
    <h1>input2</h1>
    {{ input() }}

    //带参数的宏
    {% macro input2(type,value,size) %}
        <input type="{{ type }}"  value="{{ value }}" size="{{ size }}">
    {% endmacro %}

    <h1>带参数宏</h1>
    {{ input2("text","",50) }}
</body>
</html>

  宏定义在外部

    {% macro input5() %}
        <input type="text"   size="20">
    {% endmacro %}

猜你喜欢

转载自www.cnblogs.com/tjp40922/p/11925096.html
今日推荐