创建 Jinja2 宏,将内容放在不同的位置
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您在为每个部分(即标题、正文、尾注)定义一致的结构方面正走在正确的道路上。将这些信息存储在常规 Python 数据结构中(而不是 Jinja 块和/或自定义扩展)是否可以接受?下面的例子。
在
content.jnj
中:在
template.jnj
中:请注意,
content.jnj
需要 Jinja2do
扩展名启用(例如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
:in
template.jnj
:Note that
content.jnj
requires the Jinja2do
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).