2011-09-11 56 views
0

我試圖巢龍捲風模板中使用{%包含%}:繼承蟒蛇瓦爾模板

<html> 
    {% for headline in HL['headlines'] %} 
     {% include 'hl_1.html' %} 
    {% end %} 
    </ul> 
    </body> 
</html> 

上述作品模板,以上作品的子模板。我無法弄清楚如何做的是傳入子模板的名稱(例如,在父模板的名稱空間中使用字符串參數替換'hl_1.html')。在審查template.py源代碼之後,似乎{%include接受一個字符串,而沒有其他東西。但如果可以動態地指定子模板,那將是太棒了。

有沒有人試過這個併成功了?

感謝

回答

2

達到這一目的通常是通過使用UI modules的方式。

這就是我將如何構建你的應用程序。

首先main.py

import tornado.ioloop           
import tornado.web 
import views 

class MainHandler(tornado.web.RequestHandler):     
    def get(self):            
     HL = {             
       'headlines': ['head1', 'head2', 'head3'], 
       } 
     self.render('tmpl.html', HL=HL) 

if __name__ == "__main__": 
    application = tornado.web.Application([ 
     (r"/", MainHandler), 
    ], ui_modules=views) 
    application.listen(8888) 
    tornado.ioloop.IOLoop.instance().start() 

然後你的模板tmpl.html

<html> 
    {% for headline in HL['headlines'] %} 
     {% module Headline(headline) %} 
    {% end %} 
    </ul> 
    </body> 
</html> 

最後,views.py,在這裏你可以定義你所有的UI模塊:

from tornado.web import UIModule 

class Headline(UIModule): 
    def render(self, name): 
     return '<h1>%s</h1>' % name 

UI modules就像「可重複使用的模板「,t帽子接受參數。

+0

謝謝! - 剛剛看到你的迴應。將嘗試一下。 – idiotype

+0

我不確定,我是否犯了錯誤或以上的事情與Tornado 3.1.1失敗。他們提到ui_modules應該是一個字典,在這種情況下它可以工作。 – avi