tornado中controller如何传递变量到模版或者说模版中如何调用controller变量最优化的方法是什么?
2 回答
智慧大石
TA贡献1946条经验 获得超3个赞
不记得文档出处了,从我以前写的代码里面翻到的,貌似可读性上比@KJ 以及@agassi_yzh 的方法都好那么一丢丢。
class MainHandler(tornado.web.RequestHandler):
def get(self):
items = ["Item 1", "Item 2", "Item 3"]
params = {
"title" : "Your Title",
"item" : items
}
self.render(u"template.html", **params)
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<ul>
{% for item in items %}
<li>{{ escape(item) }}</li>
{% end %}
</ul>
</body>
</html>
- 2 回答
- 0 关注
- 884 浏览
添加回答
举报
0/150
提交
取消