自定义uimodule
s3.py
import tornado.ioloop import tornado.web import UIMethod as mt class MainHandler(tornado.web.RequestHandler): def get(self): self.render("s3.html") def post(self, *args, **kwargs): self.render('s3.html') settings={ 'template_path':'tpl', 'static_path': 'static', 'ui_methods': mt, } application = tornado.web.Application([ (r"/index", MainHandler), ],**settings) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
s3.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h3>{{func()}}</h3> </body> </html>
UIMethod.py
def func(self): return '123'
自定义UIModule
s3.py
import tornado.ioloop import tornado.web import UIModule as md class MainHandler(tornado.web.RequestHandler): def get(self): self.render("s3.html") def post(self, *args, **kwargs): self.render('s3.html') settings={ 'template_path':'tpl', 'static_path': 'static', 'ui_modules': md, } application = tornado.web.Application([ (r"/index", MainHandler), ],**settings) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
s3.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h3>{% module custom()%}</h3> </body> </html>
UIModule.py
from tornado.web import UIModule from tornado import escape class custom(UIModule): def render(self, *args, **kwargs): return 123