宏
是Jinja2
特有的,像Django
則沒有這個。html
先新建一個項目macroDemo
:app
而後在templates
文件夾中新建index.html
文件,並在代碼中返回渲染後的文件:函數
而後回到index.html
,如今假設咱們要寫一個登陸的表單:測試
代碼:spa
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> <title>宏</title>
</head>
<body> <table> <tbody> <tr> <td>帳號</td> <td><input type="text" placeholder="請輸入帳號"></td> </tr> <tr> <td>密碼</td> <td><input type="password" placeholder="請輸入密碼"></td> </tr> <tr> <td></td> <td><input type="submit" value="提交"></td> </tr> </tbody> </table>
</body>
</html>
執行app.py
文件,看到:3d
看一下剛纔寫的index.html
文件,每一個標籤都傳瞭如type
,placeholder
等屬性,那麼咱們可不能夠把相同的內容提取出來呢?答案固然是能夠,這時候就要用到宏的概念。code
定義宏orm
{% macro 名稱() %} 代碼塊 {% endmacro %}
調用htm
{{ 宏名稱() }}
代碼blog
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>宏</title> {% macro input() %} <input type="text"> {% endmacro %} </head> <body> <table> <tbody> <tr> <td>帳號</td> <td>{{ input() }}</td> </tr> <tr> <td>密碼</td> <td>{{ input() }}</td> </tr> <tr> <td></td> <td>{{ input() }}</td> </tr> </tbody> </table> </body> </html>
保存一下,刷新頁面看到:
也就是說代碼已經生效了。其實能夠把宏的名稱()
當作一個函數。
可是如今input
標籤中還有type
、placeholder
等屬性,這個時候就須要給input()
傳參數了:
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> <title>宏</title> {% macro input(type, name, placeholder, value) %}
<input type="{{ type }}", name="{{ name }}", placeholder="{{ placeholder }}", value="{{ value }}"> {% endmacro %}
</head>
<body> <table> <tbody> <tr> <td>帳號</td> <td>{{ input("text", "", "請輸入帳號", "") }}</td> </tr> <tr> <td>密碼</td> <td>{{ input("text", "", "請輸入密碼", "") }}</td> </tr> <tr> <td></td> <td>{{ input("submit", "", "", "提交") }}</td> </tr> </tbody> </table>
</body>
</html>
保存,而後看到頁面:
固然,下面在傳值的時候也能夠用關鍵字參數進行傳遞,好比
{{ input(type="submit", name="", placeholder="", value="提交") }}
效果是同樣的。
若是屬性有默認值,那麼也能夠添加默認值:
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> <title>宏</title> {% macro input(type='text', name='', placeholder='', value='') %}
<input type="{{ type }}", name="{{ name }}", placeholder="{{ placeholder }}", value="{{ value }}"> {% endmacro %}
</head>
<body> <table> <tbody> <tr> <td>帳號</td> <td>{{ input(placeholder="請輸入帳號") }}</td> </tr> <tr> <td>密碼</td> <td>{{ input(placeholder="請輸入密碼") }}</td> </tr> <tr> <td></td> <td>{{ input(type="submit", value="提交") }}</td> </tr> </tbody> </table>
</body>
</html>
須要注意的是若是給了默認值,那麼傳參的時候就必須用關鍵字參數
進行傳值了。
也能夠將宏封裝成一個包的形式,在須要使用的時候經過導入進行調用:
咱們在template
文件夾下新建文件夾macros
專門存放宏文件,並在該文件夾中新建forms.html
文件。將宏的定義從index.html
文件中剪切出來,放到forms.html
文件中:
{% macro input(type='text', name='', placeholder='', value='') %}
<input type="{{ type }}", name="{{ name }}", placeholder="{{ placeholder }}", value="{{ value }}">
{% endmacro %}
而後在index.html
中只須要導入宏文件便可。導入方法:{% import 'macros/forms.html' as forms %}
注意,這裏後面必需要as
,也就是必須給導入的文件起個名字。
代碼:
{% import 'macros/forms.html' as forms %}
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> <title>宏</title>
</head>
<body> <table> <tbody> <tr> <td>帳號</td> <td>{{ forms.input(placeholder="請輸入帳號") }}</td> </tr> <tr> <td>密碼</td> <td>{{ forms.input(placeholder="請輸入密碼") }}</td> </tr> <tr> <td></td> <td>{{ forms.input(type="submit", value="提交") }}</td> </tr> </tbody> </table>
</body>
</html>
保存之後回到頁面,能夠看到效果不變。
如今forms.html
文件中只有一個宏定義,若是是多個的時候,好比把froms.html
改成:
{% macro input(type='text', name='', placeholder='', value='') %}
<input type="{{ type }}", name="{{ name }}", placeholder="{{ placeholder }}", value="{{ value }}">
{% endmacro %} {% macro textarea(name="", cols="", rows="") %}
<textarea name="{{ name }}", cols="{{ cols }}", rows="{{ rows }}"></textarea>
{% endmacro %}
這時候在index.html
導入就要用:{% from 'macros/forms.html' import input %}
或者{% from 'macros/forms.html' import input as input_field%}
代碼:
{% from 'macros/forms.html' import input %}
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> <title>宏</title>
</head>
<body> <table> <tbody> <tr> <td>帳號</td> <td>{{ input(placeholder="請輸入帳號") }}</td> </tr> <tr> <td>密碼</td> <td>{{ input(placeholder="請輸入密碼") }}</td> </tr> <tr> <td></td> <td>{{ input(type="submit", value="提交") }}</td> </tr> </tbody> </table>
</body>
</html>
執行之後頁面顯示效果不變。若是如今要把兩個宏都導入,那就只須要:
{% from 'macros/forms.html' import input, textarea %}
或者{% from 'macros/forms.html' import input as input_field, textarea%}
若是用下面這種方式,就要把input as input_field
看作一個總體。