如何简化Tornado的模块传值? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zzxworld
V2EX    Tornado

如何简化Tornado的模块传值?

  •  
  •   zzxworld 2012-08-18 02:56:33 +08:00 8255 次点击
    这是一个创建于 4859 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个简单的问题 ,表达起来还真是有点困难。大致是如下的情况:

    使用tornado做的网站中,多个页面用到了同一个菜单,菜单内容是从数据库中读取的。现在每个request handler的get方法中都有如下的内容:

    self.render(..., menu=menu(), ...)

    有什么办法可以在一个函数中搞定这种全局的模板参数,而不是在每个 get 方法中都要添加此参数?
    9 条回复    1970-01-01 08:00:00 +08:00
    jerry
        1
    jerry  
       2012-08-18 03:08:00 +08:00
    写个BaseHandler,在里面重写下render()
    fanzeyi
        2
    fanzeyi  
       2012-08-18 03:15:23 +08:00
    所有的 Request Handler 都从一个 Base Handler 继承, Base Handler 则继承 tornado.web.RequestHandler

    然后在 Base Handler 里重写 render 函数

    def render(self, *args, **kwargs):
    super(BaseHandler, self).render(menu = menu(), *args, **kwargs)

    (应该是这么写的= = 未测试)
    zhouyang
        3
    zhouyang  
       2012-08-18 07:01:11 +08:00
    可以用uimodule吧
    reorx
        4
    reorx  
       2012-08-18 09:36:24 +08:00   1
    @fanzeyi render 接受的第一个参数是 template 的文件名。。

    Lz 的需求,使用 UIModule 是很合适的,具体可以看这里:
    http://www.tornadoweb.cn/documentation#ui
    fanzeyi
        5
    fanzeyi  
       2012-08-18 11:54:36 +08:00
    @reorx 举个栗子嘛……
    reorx
        6
    reorx  
       2012-08-18 12:41:01 +08:00   1
    zzxworld
        7
    zzxworld  
    OP
       2012-08-20 10:49:43 +08:00
    @all thx.
    uimodule的确很好用,谢谢。
    kernel1983
        8
    kernel1983  
       2012-08-24 09:57:13 +08:00   1
    self.render 早就不用带一堆参数了!

    self.a = "xxx" 之后 在模版里面可以直接 {{ handler.a }}

    令人惊讶的是这些信息都写在 http://www.tornadoweb.org/documentation/overview.html 页面上,

    escape: alias for tornado.escape.xhtml_escape
    xhtml_escape: alias for tornado.escape.xhtml_escape
    url_escape: alias for tornado.escape.url_escape
    json_encode: alias for tornado.escape.json_encode
    squeeze: alias for tornado.escape.squeeze
    linkify: alias for tornado.escape.linkify
    datetime: the Python datetime module
    handler: the current RequestHandler object
    request: alias for handler.request
    current_user: alias for handler.current_user
    locale: alias for handler.locale
    _: alias for handler.locale.translate
    static_url: alias for handler.static_url
    xsrf_form_html: alias for handler.xsrf_form_html
    reverse_url: alias for Application.reverse_url
    All entries from the ui_methods and ui_modules Application settings
    Any keyword arguments passed to render or render_string

    http://www.tornadoweb.org/documentation/template.html 页面上居然只字未提

    我估计看完这个以后, 大家都去忙着修改自己现有的代码了
    703040135
        9
    703040135  
       2012-08-24 10:24:42 +08:00
    jinja2中有类似 UIModule 的方法么
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1144 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 17:15 PVG 01:15 LAX 09:15 JFK 12:15
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86