如何将选定的命名参数传递给 Jinja2 的包含上下文?

发布于 2025-01-08 09:46:08 字数 426 浏览 0 评论 0原文

使用 Django 模板引擎,我可以在使用命名参数设置自定义上下文时包含另一个部分模板,如下所示:

{% include "list.html" with articles=articles_list1 only %}
{% include "list.html" with articles=articles_list2 only %}

正如您可能假设的,articles_list1articles_list2 是两个不同的列表,但我可以重用相同的 list.html 模板,该模板将使用 articles 变量。

我正在尝试使用 Jinja2 实现相同的目标,但我看不到推荐的方法,因为不支持 with 关键字。

Using Django templating engine I can include another partial template while setting a custom context using named arguments, like this:

{% include "list.html" with articles=articles_list1 only %}
{% include "list.html" with articles=articles_list2 only %}

As you may be supposing, articles_list1 and articles_list2 are two different lists, but I can reuse the very same list.html template which will be using the articles variable.

I'm trying to achieve the same thing using Jinja2, but I can't see what's the recommended way, as the with keyword is not supported.

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

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

发布评论

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

评论(5

留一抹残留的笑 2025-01-15 09:46:08

Jinja2 有 with 关键字 - 它不会'它不会为您提供与 Django 相同的语法,它可能不会按照您预期的方式工作,但您可以这样做:

{% with articles=articles_list1 %}
    {% include "list.html" %}
{% endwith %}
{% with articles=articles_list2 %}
    {% include "list.html" %}
{% endwith %}

但是,如果 list.html 基本上只是作为创建列表的一种方式,那么您可能想将其更改为 相反 - 这将为您提供更大的灵活性。

{% macro build_list(articles) %}
    <ul>
        {% for art in articles %}
            <li>{{art}}</li>
        {% endfor %}
    </ul>
{% endmacro %}

{# And you call it thusly #}
{{ build_list(articles_list1) }}
{{ build_list(articles_list2) }}

要从另一个模板使用此宏,请导入它:

{% from "build_list_macro_def.html" import build_list %}

Jinja2 has the with keyword - it won't give you the same syntax as Django, and it may not work the way you anticipate but you could do this:

{% with articles=articles_list1 %}
    {% include "list.html" %}
{% endwith %}
{% with articles=articles_list2 %}
    {% include "list.html" %}
{% endwith %}

However, if list.html is basically just functioning as a way to create a list then you might want to change it to a macro instead - this will give you much more flexibility.

{% macro build_list(articles) %}
    <ul>
        {% for art in articles %}
            <li>{{art}}</li>
        {% endfor %}
    </ul>
{% endmacro %}

{# And you call it thusly #}
{{ build_list(articles_list1) }}
{{ build_list(articles_list2) }}

To use this macro from another template, import it:

{% from "build_list_macro_def.html" import build_list %}
跨年 2025-01-15 09:46:08

这样你就可以将多个变量传递给Jinja2 Include语句 - (通过在With语句中用逗号分隔变量):

            {% with var_1=123, var_2="value 2", var_3=500 %}
                {% include "your_template.html" %}
            {% endwith %}

This way you can pass multiple variables to Jinja2 Include statement - (by splitting variables by comma inside With statement):

            {% with var_1=123, var_2="value 2", var_3=500 %}
                {% include "your_template.html" %}
            {% endwith %}
花伊自在美 2025-01-15 09:46:08

对于 2017 年以上的读者,自 2.9 开始,Jinja 默认包含 with 语句。无需延期。

http://jinja.pocoo.org/docs/2.9/templates/#with -声明

在旧版本的 Jinja(2.9 之前)中,需要通过扩展来启用此功能。现在默认启用它。

For readers in 2017+, Jinja as of 2.9 includes the with statement by default. No extension necessary.

http://jinja.pocoo.org/docs/2.9/templates/#with-statement

In older versions of Jinja (before 2.9) it was required to enable this feature with an extension. It’s now enabled by default.

蘸点软妹酱 2025-01-15 09:46:08

更新2021+

默认情况下,包含的模板可以访问活动上下文的变量。有关导入和包含的上下文行为的更多详细信息,请参阅导入上下文行为< /a>.

从 Jinja 2.2 开始,您可以使用忽略缺失来标记包含;在这种情况下,如果要包含的模板不存在,Jinja 将忽略该语句。当与 withwithout context 结合使用时,它必须放置在上下文可见性语句之前。以下是一些有效的示例:

{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with context %}
{% include "sidebar.html" ignore missing without context %}

Updated 2021+

Included templates have access to the variables of the active context by default. For more details about context behavior of imports and includes, see Import Context Behavior.

From Jinja 2.2 onwards, you can mark an include with ignore missing; in which case Jinja will ignore the statement if the template to be included does not exist. When combined with with or without context, it must be placed before the context visibility statement. Here are some valid examples:

{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with context %}
{% include "sidebar.html" ignore missing without context %}
美男兮 2025-01-15 09:46:08

另一种无需插件的选择是使用宏并将其包含在另一个文件中:

文件 ma​​cro.j2

{% macro my_macro(param) %}
  {{ param }}
{% endmacro %}

文件 ma​​in.j2< /em>

{% from 'macro.j2' import my_macro %}

{{ my_macro(param) }}

Another option, without plugins, is to use macros and include them from another file:

file macro.j2

{% macro my_macro(param) %}
  {{ param }}
{% endmacro %}

file main.j2

{% from 'macro.j2' import my_macro %}

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