flask 模板 -- 宏

定义宏

    在templates文件下新建_macros.html    

{% macro m_input(name, value='', type='text')%}
    <input type="{{ type }}" value="{{ value }}" name="{{ name }}">
{% endmacro %}

导入和使用

{% from '_marcos.html' import m_input %}
{#{% from '_marcos.html' import m_input as input_field%}  取别名,调用时使用input_field#}
{#import '_macros.html' as macros   调用 macros.m_input#}
{#导入时使用绝对路径#}
{#宏中使用视图函数传入的数据导入格式 {% from '_marcos.html' import m_input as input_field% with context}#}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table>
        <tr>
            <td>用户名:</td>
            <td>{{ m_input("username") }} </td>
        </tr>
        <tr>
            <td>密码:</td>
            <td>{{ m_input("password", type="password") }} </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>{{ m_input(type="submit", value="提交") }} </td>
        </tr>
    </table>

</body>
</html>

 


版权声明:本文为qq_36255988原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。