在 Jinja2 中,将所有键设置为字典的值的最简单方法是什么?
我有一个仪表板,它为每个仪表板项目的上下文命名空间。有没有一种快速方法可以将字典的所有值设置为模板中的键?
我想重用模板,而不是总是为我的变量命名空间。
我的上下文可以简化为如下所示:
{
"business": {"businesses": [], "new_promotions": []},
"messages": {"one_to_one": [], "announcements": []
}
因此,在 with 语句中,我想将所有业务项设置为本地包含。目前要执行此操作,我必须单独设置每个变量:
{% with %}
{% set businesses = business["businesses"] %}
{% set new_promotions = business["new_promotions"] %}
{% include "businesses.html" %}
{% endwith %}
我尝试过:
{% with %}
{% for key, value in my_dict %}
{% set key = value %}
{% endfor %}
{% include "businesses.html" %}
{% endwith %}
但它们仅在 for 循环中具有作用域,因此不在包含范围内...
I've got a dashboard that namespaces the context for each dashboard item. Is there a quick way I can set all the values of a dictionary to the keys in a template?
I want to reuse templates and not always namespace my variables.
My context can be simplified to look something like this:
{
"business": {"businesses": [], "new_promotions": []},
"messages": {"one_to_one": [], "announcements": []
}
So in a with statement I want to set all the business items to be local for the including. To do this currently I have to set each variable individually:
{% with %}
{% set businesses = business["businesses"] %}
{% set new_promotions = business["new_promotions"] %}
{% include "businesses.html" %}
{% endwith %}
I tried:
{% with %}
{% for key, value in my_dict %}
{% set key = value %}
{% endfor %}
{% include "businesses.html" %}
{% endwith %}
But they only have scope in the for loop so aren't scoped in the include...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
长话短说:您不能在上下文中设置任意变量。
{% set key = value %}
只是将名为key
的变量设置为给定值。原因是 Jinja2 将模板编译为 Python 代码。 (如果您想查看模板生成的代码,请从 http://ryshcate.leafstorm.html 下载脚本。 us/paste/71c95831ca0f1d5 并将模板的文件名传递给它。)为了加快处理速度,它会为模板中使用的每个变量创建局部变量(仅在第一次遇到该变量时在上下文中查找该变量) ),与 Django 不同,Django 使用上下文进行所有变量查找。
为了正确生成此代码,它需要能够跟踪在任何给定时间存在哪些局部或全局变量,以便它知道何时在上下文中查找。设置随机变量会阻止其工作,这就是为什么不允许 contextfunction 修改上下文,而只能查看它。
不过,我要做的不是让您的业务显示代码成为包含的模板,而是让它成为另一个模板中的宏。例如,在
businesses.html
中:然后在主模板中:
或者,更好的是,将它们分成两个单独的宏 - 一个用于企业,另一个用于新促销。您可以在 http://bitbucket.org 看到很多很酷的模板技巧/plurk/solace/src/tip/solace/templates/,当然还要查看 Jinja2 文档 http://jinja.pocoo.org/2/documentation/templates。
Long story short: you can't set arbitrary variables in the context. The
{% set key = value %}
is just setting the variable namedkey
to the given value.The reason is because Jinja2 compiles templates down to Python code. (If you want to see the code your template generates, download the script at http://ryshcate.leafstorm.us/paste/71c95831ca0f1d5 and pass it your template's filename.) In order to make processing faster, it creates local variables for every variable you use in the template (only looking up the variable in the context the first time it's encountered), as opposed to Django, which uses the context for all variable lookups.
In order to generate this code properly, it needs to be able to track which local or global variables exist at any given time, so it knows when to look up in the context. And setting random variables would prevent this from working, which is why
contextfunction
s are not allowed to modify the context, just view it.What I would do, though, is instead of having your business-displaying code be an included template, is have it be a macro in another template. For example, in
businesses.html
:And then in your main template:
Or, better yet, separate them into two separate macros - one for businesses, and one for new promotions. You can see a lot of cool template tricks at http://bitbucket.org/plurk/solace/src/tip/solace/templates/, and of course check the Jinja2 documentation at http://jinja.pocoo.org/2/documentation/templates.
我找到了一个解决方法 - 通过创建上下文函数,我可以渲染模板并直接设置上下文或合并上下文(但不确定这是一个好的做法)。
所以我的模板看起来像:
I've found a work around - by creating a context function I can render the template and directly set the context or merge the context (not sure thats good practise though).
So my templates look like: