I want to use pure jinja2 (no frame) to make a web page template output in the form of articles. There is a basic skeleton base.html, with a large number of chapters, which will be freely combined and put into the skeleton. If these chapters are put in macro, it will be very troublesome, because there are some reused picture table elements in chapters that also need to be placed in macro, but these chapters are also very difficult to do with block. Because block cannot introduce the skeleton with import and cannot pass parameters yet.
I saw on the Internet that block, can be called with the .clients property of template, so I made the following attempt:
-sharpconfig.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"}}
]}
-sharpbase.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 }}{{ chapter.title }}</h2>
{%- if chapter.children %}
{%- for section in chapter.children %}
<h3>{{ ch_loop.index }}.{{ loop.index }}{{ 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 %}
-sharptemplate1.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 %}
-sharpcomponent.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 }}
{% endif %}
{% endmacro %}
{% macro common_img(src, alt, name) %}
<img src="{{ src }}" alt="{{ alt }}">
{% if name %}
<b class="imgname">{{ name }}</b>
{% endif %}
{% endmacro %}
but jinja2 reported the following error:
jinja2.exceptions.UndefinedError: "jinja2.environment.TemplateModule object" has no attribute "blocks"
I looked at the source code of jinja2 and found that it seems possible to call the block, of the Template object with the .upload attribute in the python script, but not in the template, because the import in the template is TemplateModule class, which has no blocks attribute and does not seem to output block. So how do I introduce block into my skeleton, or do I need to modify the way I build templates?