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:
{
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>
The documentation page states:
*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*:
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]).
regards
GA
I got around this by doing the following in the template file:
views/index.tpl
mydictfile:
This seems to be working for me.