1. 程式人生 > >Templates中的macro和include標籤

Templates中的macro和include標籤

1.macro標籤
 1.作用:相當於在模板中聲名函式

 2.使用方法:

  語法:{% macro 名稱(引數列表) %}

       xxx

     {% endmacro %}

建立 macro.html 模板檔案   -->  作用:定義專案中要用到的所有的巨集

{% macro show_li(str) %}
    <li style="background:#f60;">{{str}}</li>
{% endmacro %}

在使用的網頁中,匯入 macro.html
{% import 'macro.html' as macros %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!-- 先將存放巨集的html匯入-->
    {% import 'macro.html' as macros %}

    <ul>
        {% for str in params.list %}
            <!-- 呼叫巨集裡面寫好的方法-->
            {{macros.show_li(str)}}
        {
% endfor %} </ul> </body> </html>

2.include標籤

將其他的模板檔案的所有內容引用到當前的模板檔案中
語法:{% include 'xxx.html' %}