在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  Python  網(wǎng)絡(luò)安全  HTML/ jinja2 如何在一個模板引入另一個模板中的block

jinja2 如何在一個模板引入另一個模板中的block

我想用純jinja2(不用框架)做一個輸出以文章形式為主體的網(wǎng)頁模板,有一個基礎(chǔ)的骨架base.html,有大量的章節(jié),這些章節(jié)會自由組合放入骨架中,如果這些章節(jié)用macro放將會很麻煩,因為章節(jié)中有一些重復(fù)使用的圖片表格元素也需要放在macro中,但是這些章節(jié)用block也很不好辦,因為block不能用import引入骨架并且還不能傳參數(shù)。

我在網(wǎng)上看到好像可以用template的.blocks屬性調(diào)用block,所以有了以下嘗試:

#config.json
{"templates":[
        {"title":"chapter1",
        "children":[
                {"title":"chapter1_1","template":"template1.html","block":"block1"},
                {"title":"chapter1_2","template":"template2.html","block":"block1","parameter":{"note":"hello world"}},
        ]},
        {"title":"chapter2","template":"template1.html","block":"block2","parameter":{"note":"blabla"}}
]}


#base.html
{% from "component.html" import common_table as common_table with context %}
{% from "component.html" import common_img as common_img with context %}
{%- for chapter in chapter_list %}
        {%- set ch_loop = loop %}
        <h2>{{ ch_loop.index }}&nbsp;{{ chapter.title }}</h2>
        {%- if chapter.children %}
                {%- for section in chapter.children %}
                        <h3>{{ ch_loop.index }}.{{ loop.index }}&nbsp;{{ section.title }}</h3>
                        {% import section.template as template with context %}
                        {% set parameter=section.parameter %}
                        {{ template.blocks[section.block] }}
                {%- endfor %}
        {%- else %}
                {% import chapter.template as template with context %}
                {% set parameter=chapter.parameter %}
                {{ template.blocks[chapter.block] }}
        {%- endif %}
{%- endfor %}

#template1.html 
{% block block1 %}
        I am block 1
        {{ common_table(id="table1",head="nohead")  }}
        {{ common_img(src="css/icon/icon1.png")  }}
{% endblock %}

{% block block2 %}
        I am block 2
        {{ common_img(src="css/icon/icon2.png")  }}
        {% if parameter.note=="blabla" %}
                {{ "blabla~blabla~" }}
        {% endif %}
{% endblock %}

#component.html
{% macro common_table(id, name,note, head="onehead") %}
        {% if name %}
                <b class="tablename">{{ name }}</b>
        {% endif %}
        <table id="{{ id }}" class=" {{ head }}"></table>
        {% if note %}
                 <p class="tablenote">{{ note }}</p>
        {% endif %}
{% endmacro %}

{% macro common_img(src, alt, name) %}
        <img  src="{{ src }}" alt="{{ alt }}">
        {% if name %}
                <b class="imgname">{{ name }}</b>
        {% endif %}
{% endmacro %}

但是jinja2報了以下錯誤:

jinja2.exceptions.UndefinedError: 'jinja2.environment.TemplateModule object' has no attribute 'blocks'

我查看了jinja2的源代碼,發(fā)現(xiàn)在python腳本中好像可以用.blocks屬性調(diào)用Template對象的block,但是在模板中不行,因為模板中import進(jìn)來的是TemplateModule class,這個類沒有blocks屬性,好像也不輸出block。那么請問我該如何在骨架中引入block呢,還是需要修改我的構(gòu)建模板的方法?

回答
編輯回答
故林

為什么不用include

2018年2月26日 12:44
編輯回答
傲嬌范

已解決,解決辦法是章節(jié)使用macro,重復(fù)使用的圖表元素使用jquery添加

2018年8月13日 10:15