如何在Jinja2模板中访问会话数据(应用引擎上的Bottle框架)?
1 个回答
11
如果你想让一些东西在所有模板中都能用,可以把它们添加到 Jinja2 的 全局环境 中。想了解更多信息,可以查看 这个页面。
更新:
这里有一个简单的例子,关于你的设置代码:
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
然后,在处理请求的代码中:
env.globals['session'] = session # Your session
# Your template can contain things like {{ session['key'] }}
template = env.get_template('mytemplate.html')
print template.render(the='variables', go='here')
#return response using rendered data