Tornado templates模板渲染语法总结

Edit

Tornado templates模板渲染语法总结

工作需要研究了一下tornado的模板渲染语法,写了个测试,总结了一下相关语法,聊作笔记如下。

tornado本身非常灵活,支持几乎所有python支持的模板语言。除此之外,它本身也提供了一个轻量级的模板,放在tornado.template中。

测试用例

代码清单1:tornado_template.py

import tornado.httpserver
import tornado.ioloop
import tornado.web

# define one "add" customization funcation which will be used in the assigned template file.
def add(x, y):
    return (x+y)

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        items = ["item1","item2","item3"]
        # render the corresponding template file, and pass the "**args" to the assigned template
        # not only we can pass the realted parameters, but also can pass the related functions.
        # so extendible and powerful! :)
        items2 = ["item1", "item2"]
        def checked(item):
            return 'checked=checked' if item in items2 else ''
        self.render("template_test.html", items=items, add=add, items2=items2, checked=checked)

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8081)
    tornado.ioloop.IOLoop.instance().start()

代码清单2:template_test.html

<html>
<head>
<title>template function test</title>
</
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值