【发布时间】:2012-07-21 18:46:39
【问题描述】:
我正在使用python的bottle框架来开发一个简单的网页。我无法理解如何将字典传递给子模板。示例代码:
我的字典:
{
message: "Hello World",
...other template vars ...
}
Router.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>
*%include 语句:您可以使用 %include sub_template [kwargs] 语句包含其他模板。 sub_template 参数 指定要包含的模板的名称或路径。其余的 该行被解释为以逗号分隔的 key=statement 列表 类似于函数调用中的关键字参数的对。他们通过 到类似于 SimpleTemplate.render() 调用的子模板。这 **也允许传递 dict 的 kwargs 语法*:
但是,没有给出如何将带有此 **kwargs 的字典传递给子模板的示例。有人做过吗?如果我只是说 %include subpage1 mydictionary,bottle 会抱怨 mydictionary 未定义(即使 mydictionary 是全局字典 [在 Router.py 中定义])。
问候 遗传算法
【问题讨论】: