将变量传递到 jinja 导入或包含父 html 文件

发布于 2024-12-22 21:12:51 字数 540 浏览 2 评论 0原文

场景是:

“你有一个名为 person 的变量,其中包含许多字段,如姓名、地址等,你想将其传递给部分 html” - 该解决方案可能是搜索客户的结果,例如

代码片段.html

<div id="item">
  <ul>
     <li>
         <span>{{name}}</span>
         <span>{{address}}</span>
     <li>
  </ul>
</div>

mypage.html

<div id="result">
   {% include "snippet.html" passing {{person}} %}
</div>

实现此目的的最佳方法是什么。在文档中,它讨论了在各处传递上下文,但在我看来,渲染模板时这似乎是一个相当大的对象。将特定对象传递到每个模板中肯定更容易吗?

The scenario would be:

"you have a variable called person which contains a number of fields like name, address, etc which you want to pass to a partial piece of html" - this solution could be results from a search for customers for example

snippet.html

<div id="item">
  <ul>
     <li>
         <span>{{name}}</span>
         <span>{{address}}</span>
     <li>
  </ul>
</div>

mypage.html

<div id="result">
   {% include "snippet.html" passing {{person}} %}
</div>

What is the best way to achieve this. In the documentation it talks about passing context around everywhere, but that seems to me to be a rather large object when rendering templates. surely it is easier to pass specific objects into each template?

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

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

发布评论

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

评论(2

风苍溪 2024-12-29 21:12:51

当您将一个模板包含到另一个模板中时,它可以访问其上下文,因此如果您将 person 变量传递给 mypage.html 的上下文,您将能够从导入的模板访问它,如下所示:

snippet.html:

<div id="item">
    <ul>
        <li>
            <span>{{ person.name }}</span>
            <span>{{ person.address }}</span>
        </li>
    </ul>
</div>

mypage.html:

<div id="result">
    {% include 'snippet.html' %}
</div>

view.py:

def view(person_id):
    person = Person.get(person_id) # or whatever source you get your data from
    return render_template('mypage.html', person=person)

When you include a template into another one, it gains access to its context, so if you pass your person variable to mypage.html's context, you'll be able to access it from your imported template like this:

snippet.html:

<div id="item">
    <ul>
        <li>
            <span>{{ person.name }}</span>
            <span>{{ person.address }}</span>
        </li>
    </ul>
</div>

mypage.html:

<div id="result">
    {% include 'snippet.html' %}
</div>

view.py:

def view(person_id):
    person = Person.get(person_id) # or whatever source you get your data from
    return render_template('mypage.html', person=person)
柳絮泡泡 2024-12-29 21:12:51

这补充了这个答案,作者是 mdeous.

环境全局变量在宏中始终可用,但上下文变量则不然。
要在导入的宏中使用上下文,您必须在导入时使用with context,例如:

{% from "your_macros.html" import your_macro with context %}

This complements this answer by mdeous.

Environment globals are always available in macros, but context variables are not.
To have the context available in an imported macro, you have to use with context when importing, e.g.:

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