tonado custom template ui_methods, the use of ui_modules

Keywords: Attribute xml github Python

First of all, we have a tornado hello_world.
New test.py, tem.html, code as follows:

test.py

import tornado.web
import tornado.ioloop

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("tem.html")
application = tornado.web.Application([
    (r'/' , MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

tem.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
    <body>
        hello_world
    </body>
</html>

—————————— The dividing line
Then you need to modify the demo, using ui_methods, ui_modules.
First of all, I would like to introduce:
ui_methods custom method functions. Used in templates as a general method.
ui_modules customize a component to be used in templates
Next we add ui_methods, ui_modules for demo.
Create a new uimethod.py, and define a method in this file

uimethod.py

def say_hello(self , arg):
    return 'hello' + arg

Modify the test.py file
Import the uimehod we wrote above, pass a parameter name d Lili to render, and add "ui_methods" to settings

test.py

import tornado.web
import tornado.ioloop
import uimethod

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("tem.html" , name = "Lili")

settings = {
    "ui_methods" : uimethod ,
}

application = tornado.web.Application([
    (r'/' , MainHandler),
] , **settings)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

Modify the tem.html file using ui_methods:
tem.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>{{name}}</h1>
    <h1>{{ say_hello(name) }}</h1>
</body>
</html>

Run the program, access localhost:8888, the results are as follows

—————————— The dividing line
Then use ui_modules
Create a new uimethod.py, and define a method in this file

ui_modules.py

from tornado.web import UIModule

class talk(UIModule):
    def render(self , name):
        return "Have a good day " + name

Modify the test.py file
Import the UI module we wrote above and add "ui_methods" to settings

test.py

import tornado.web
import tornado.ioloop
import uimethod
import uimodule
class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("tem.html" , name = "Lili")

settings = {
    "ui_methods" : uimethod ,
    "ui_modules" : uimodule
}

application = tornado.web.Application([
    (r'/' , MainHandler),
] , **settings)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

Modify the tem.html file using ui_modules:
tem.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>{{name}}</h1>
    <h1>{{ say_hello(name) }}</h1>
    <h1>{% module talk(name) %}</h1>
</body>
</html>

Run the program, access localhost:8888, the results are as follows

With ui_methods, ui_modules can use python code to complete logic, and tornado's function is very useful.

The link to this demo is https://github.com/dangsh/pythonPra/tree/master/tornadoPra/uiPra
You can check it if you need it.

Posted by LightningSt on Tue, 05 Feb 2019 23:42:17 -0800