首页 > 其他分享 >flask-宏的用法macro

flask-宏的用法macro

时间:2023-03-17 20:13:18浏览次数:46  
标签:flask macro value 用法 引入 input type

宏---类似函数

macro-demo.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>宏定制</title>
</head>
<body>
<h3>JinJa模板宏用法:(类似定义函数)</h3>
<!--定义了宏input,name,value,type是宏的参数.-->
{% macro input(name,value='',type='text') %}
    <input type="{{type}}" value="{{value|escape}}" name="{{name}}" 
	placeholder="{{name}}">
{% endmacro %}

<!--使用宏-->
<form>
    {{input('username')}}
    {{input('password',type='password')}}
    {{input('btn',type='submit',value='提交')}}
</form>

</body>
</html>

引入宏文件

可以引入其他文件中定义的宏来使用.语法类似python中的import

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>从其他文件引入宏</title>
</head>
<body>
<h3>从其他文件引入宏</h3>

{% from 'macro-demo.html' import input as input  %}
{{ input('uname') }}
{{ input('age') }}
{{ input('btn',value='提交',type='submit') }}

</body>
</html>

标签:flask,macro,value,用法,引入,input,type
From: https://www.cnblogs.com/unity-yancy/p/17228004.html

相关文章