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 關注
- 948 瀏覽
添加回答
舉報
0/150
提交
取消