问题描述
我正在使用python的bottle框架来开发一个简单的网页。我无法理解如何将字典传递给子模板。示例代码:
I am using python's bottle framework to develop a simple web page. I am having trouble understanding how to pass a dictionary to a subtemplate. Example code:
mydictionary:
mydictionary:
{
message: "Hello World",
...other template vars ...
}
Router.py
Router.py
@route('/index.html')
@view('index.tpl')
def index():
return mydictionary
视图/索引.tpl
<body>
%include subpage1 ...... <-- need to pass mydictionary here
...other stuff ...
</body>
views / subpage1.tpl
views/subpage1.tpl
<div>This is a test: {{message}}</div>
指出:
但是,没有给出如何将带有** kwargs的字典传递给子模板。有人做过吗?如果我只是说%include subpage1 mydictionary,瓶子会抱怨mydictionary是未定义的(即使mydictionary是全局dict [在Router.py中定义])。
However, no example is given on how to pass dictionary with this **kwargs to subtemplates. Anyone ever done this? If I just say %include subpage1 mydictionary, bottle complains mydictionary is undefined (even though mydictionary is a global dict [defined in Router.py]).
考虑
GA
regardsGA
推荐答案
通过在模板文件中执行以下操作:
I got around this by doing the following in the template file:
views / index.tpl
views/index.tpl
<body>
%from mydictfile import * <-- importing mydict here
%include subpage1 mydict
...other stuff ...
</body>
mydictfile:
mydictfile:
mydict = {
message: "Hello World",
...other template vars ...
}
这似乎对我有用。
这篇关于python瓶框架-如何将字典传递给嵌套模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!