创建 Jinja2 宏,将内容放在不同的位置

发布于 2024-09-02 07:15:05 字数 1294 浏览 7 评论 0原文

我想在 Jinja2 模板中创建目录和尾注。怎样才能完成这些任务呢?

例如,我想要一个如下的模板:

 {% block toc %}
 {# ... the ToC goes here ... #}
 {% endblock %}

 {% include "some other file with content.jnj" %}

 {% block endnotes %}
 {# ... the endnotes go here ... #}
 {% endblock %}

其中 其他包含 content.jnj 的文件 的内容如下:

{% section "One" %}
Title information for Section One (may be quite long); goes in Table of Contents
...
Content of section One

{% section "Two" %}
Title information of Section Two (also may be quite long)

<a href="#" id="en1">EndNote 1</a> 
<script type="text/javsacript">...(may be reasonably long)
</script> {# ... Everything up to here is included in the EndNote #}

我说“可能相当/合理长”的地方是说它不能合理地将其放入引号中作为宏或全局函数的参数。

我想知道在 Jinja2 的框架内是否有一种模式可以适应这种情况。

我最初的想法是创建一个扩展,这样就可以有一个用于部分和尾注的块,如下所示:

{% section "One" %}
Title information goes here.
{% endsection %}

{% endnote "one" %}
<a href="#">...</a>
<script> ... </script>
{% endendnote %}

然后有全局函数(在 Jinja2 环境中传递):

{{ table_of_contents() }}

{% include ... %}

{{ endnotes() }}

但是,虽然这适用于尾注,但我假设它需要第二次通过目录的内容。

感谢您的阅读。我非常感谢您的想法和意见。

布莱恩

I want to create a table of contents and endnotes in a Jinja2 template. How can one accomplish these tasks?

For example, I want to have a template as follows:

 {% block toc %}
 {# ... the ToC goes here ... #}
 {% endblock %}

 {% include "some other file with content.jnj" %}

 {% block endnotes %}
 {# ... the endnotes go here ... #}
 {% endblock %}

Where the some other file with content.jnj has content like this:

{% section "One" %}
Title information for Section One (may be quite long); goes in Table of Contents
...
Content of section One

{% section "Two" %}
Title information of Section Two (also may be quite long)

<a href="#" id="en1">EndNote 1</a> 
<script type="text/javsacript">...(may be reasonably long)
</script> {# ... Everything up to here is included in the EndNote #}

Where I say "may be quite/reasonably long" I mean to say that it can't reasonably be put into quotes as an argument to a macro or global function.

I'm wondering if there's a pattern for this that may accommodate this, within the framework of Jinja2.

My initial thought is to create an extension, so that one can have a block for sections and end-notes, like-so:

{% section "One" %}
Title information goes here.
{% endsection %}

{% endnote "one" %}
<a href="#">...</a>
<script> ... </script>
{% endendnote %}

Then have global functions (that pass in the Jinja2 Environment):

{{ table_of_contents() }}

{% include ... %}

{{ endnotes() }}

However, while this will work for endnotes, I'd presume it requires a second pass by something for the table of contents.

Thank you for reading. I'd be much obliged for your thoughts and input.

Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2024-09-09 07:15:06

似乎您在为每个部分(即标题、正文、尾注)定义一致的结构方面正走在正确的道路上。将这些信息存储在常规 Python 数据结构中(而不是 Jinja 块和/或自定义扩展)是否可以接受?下面的例子。

content.jnj 中:

{% set sections = [] %}
{% do sections.append({
'title': 'Section One title',
'body': 
"""
Section one main body text...
""",
'endnotes': 
"""
<a href='#'>...</a>
<script> ... </script>
""",
})
%}
{# append more sections #}

template.jnj 中:

{% from 'content.jnj' import sections as sections %}
{# table of contents #}
{% for section in sections %}
    {{ loop.index }}. {{ section['title'] }}
{% endfor %}
{# body #}
{% for section in sections %}
    {{ section['title'] }}
    {{ section['body'] }}
{% endfor %}
{# endnotes #}
{% for section in sections %}
    {{ loop.index }}. {{ section['endnotes'] }}
{% endfor %}

请注意,content.jnj 需要 Jinja2 do 扩展名启用(例如 env = jinja2.Environment(extensions=['jinja2.ext.do'])。)

这对于您的目的来说可能有点过分,但另一种选择是以标记语言存储内容,如reStructuredText,并将表现层设计为 Sphinx 主题(Jinja2 是默认模板格式)。

Seems like you are heading down the right path in defining a consistent structure for each section (i.e title, body, endnotes). Would storing this information in regular Python data structures - rather than Jinja blocks and/or custom extensions - be acceptable? Example below.

in content.jnj:

{% set sections = [] %}
{% do sections.append({
'title': 'Section One title',
'body': 
"""
Section one main body text...
""",
'endnotes': 
"""
<a href='#'>...</a>
<script> ... </script>
""",
})
%}
{# append more sections #}

in template.jnj:

{% from 'content.jnj' import sections as sections %}
{# table of contents #}
{% for section in sections %}
    {{ loop.index }}. {{ section['title'] }}
{% endfor %}
{# body #}
{% for section in sections %}
    {{ section['title'] }}
    {{ section['body'] }}
{% endfor %}
{# endnotes #}
{% for section in sections %}
    {{ loop.index }}. {{ section['endnotes'] }}
{% endfor %}

Note that content.jnj requires the Jinja2 do extension to be enabled (e.g. env = jinja2.Environment(extensions=['jinja2.ext.do']).)

It may be overkill for your purposes, but another option is to store the content in a markup language, such as reStructuredText, and design the presentation layer as a Sphinx theme (Jinja2 is the default template format).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文