Flask框架从入门到精通之模板宏(十九)

知识点: 一、宏的基本使用html

1、概况

在Flask的模板中有一个特性和Django内不一样,这个特性就是宏。宏的功能和python中的函数相似。python

声明宏

{% macro 宏的名字(参数) %}

​ 内容

{% endmacro %}
复制代码

调用宏

{{  宏的名字(参数)   }}
复制代码

在python函数能够实现代码复用的做用,在模板中宏也有相似的做用。浏览器

2、使用

建立一个Flask项目,并在模型声明以下代码:函数

  • 无参宏
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

{#声明#}
{% macro macro_input() %}

    输入框:<input type="text"> <br>
{% endmacro %}

{#调用#}

{{ macro_input() }}
{{ macro_input() }}
</body>
</html>
复制代码

咱们在浏览器调试一下: ui

在这里插入图片描述

  • 有参宏
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

{#声明#}
{% macro macro_input(tip,type,name) %}

    {{ tip }}<input type="{{ type }}" name="{{ name }}"> <br>
{% endmacro %}

{#调用#}

{{ macro_input('帐号:','text','email') }}
{{ macro_input('密码:','password','pwd') }}
</body>
</html>
复制代码

在这里插入图片描述

  • 缺省宏
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

{#声明#}
{% macro macro_input(tip,type,name,value='123') %}

    {{ tip }}<input type="{{ type }}" name="{{ name }}" value="{{ value }}"> <br>
{% endmacro %}

{#调用#}

{{ macro_input('帐号:','text','email') }}
{{ macro_input('密码:','password','pwd','123456789') }}
</body>
</html>
复制代码

在这里插入图片描述

3、导入宏

宏的定义能够声明在另外一个html中,而后经过import这种方式导入进来使用。新建一个html文件,声明以下代码:spa

{#声明#}
{% macro macro_input(tip,type,name,value='123') %}

    {{ tip }}<input type="{{ type }}" name="{{ name }}" value="{{ value }}"> <br>
{% endmacro %}
复制代码

注意虽然是html文件,可是没有html文件的结构。scala

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>


{% from 'common_macro.html' import macro_input %}

{#别名#}
{#{% from 'common_macro.html' import macro_input as input %}#}

{#调用#}

{{ macro_input('帐号:','text','email') }}
{{ macro_input('密码:','password','pwd','123456789') }}
</body>
</html>
复制代码

4、宏的内部变量

  • varargs : 这是一个列表。若是调用宏时传入的参数多于宏声明时的参数,多出来的没指定参数名的参数就会保存在这个列表中。3d

  • kwargs : 这是一个字典。若是调用宏时传入的参数多于宏声明时的参数,多出来的指定了参数名的参数就会保存在这个字典中。调试

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>


{#声明#}
{% macro macro_input(tip,type,name,value='123') %}

    {{ tip }}<input type="{{ type }}" name="{{ name }}" value="{{ value }}"> <br>

    <br>{{ varargs }}
    <br> {{ kwargs }}<br>
{% endmacro %}

{{ macro_input('帐号:','text','email',11,22,33,44,age=12) }}
{{ macro_input('密码:','password','pwd','123456789') }}
</body>
</html>
复制代码

咱们在浏览器调试一下: code

在这里插入图片描述

欢迎关注个人公众号:

image
相关文章
相关标签/搜索