我正在使用 python 的 bottle 框架开发一个简单的网页。我无法理解如何将字典传递给子模板。示例代码:
我的词典:
{
message: "Hello World",
...other template vars ...
}
路由器.py
@route('/index.html')
@view('index.tpl')
def index():
return mydictionary
views/index.tpl
<body>
%include subpage1 ...... <-- need to pass mydictionary here
...other stuff ...
</body>
views/subpage1.tpl
<div>This is a test: {{message}}</div>
*The %include Statement: You can include other templates using the %include sub_template [kwargs] statement. The sub_template parameter specifies the name or path of the template to be included. The rest of the line is interpreted as a comma-separated list of key=statement pairs similar to keyword arguments in function calls. They are passed to the sub-template analogous to a SimpleTemplate.render() call. The **kwargs syntax for passing a dict is allowed too*:
但是,没有给出关于如何将带有此 **kwargs 的字典传递给子模板的示例。有人做过这个吗?如果我只是说 %include subpage1 mydictionary,bottle 会提示 mydictionary 未定义(即使 mydictionary 是全局字典 [在 Router.py 中定义])。
问候 遗传算法
请您参考如下方法:
我通过在模板文件中执行以下操作来解决这个问题:
views/index.tpl
<body>
%from mydictfile import * <-- importing mydict here
%include subpage1 mydict
...other stuff ...
</body>
我的字典文件:
mydict = {
message: "Hello World",
...other template vars ...
}
这似乎对我有用。
