ui_modules和ui_method

## 06ui.py
#coding:utf-8
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from  tornado.options import define,options
import util.ui_methods
import util.ui_modules
define('port',default=8000,help='run port',type=int)
define('version',default='0.0.1',help='version 0.0.1',type=str)


class Calculation():
    def sum(self, a, b):
        return a + b

class UiHandler(tornado.web.RequestHandler):

    def func(self):
        return 'nanian'
    def get(self):
        username=self.get_argument('name','no')
        self.render('07module.html',
                    username=username,
                    fun=self.func,
                    calc=Calculation,

                    )


if __name__ == "__main__":
    tornado.options.parse_command_line()
    # print(options.port)
    app=tornado.web.Application(
        handlers=[
            (r'/ui',UiHandler),
        ],
        template_path='templates',
        static_path='static',
        ui_methods=util.ui_methods,
        ui_modules=util.ui_modules,
        #ui_modules={'Uimodule':util.ui_modules.Uimodule,'Advertisement':util.ui_modules.Advertisement},
        debug=True,
        #autoescape=None,   #关闭自动转义 全局的
    )
 #固定写法:
    http_server=tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
    
## ui_modules.py
'''
this is module
'''
from tornado.web import    UIModule
#类必须继承UIModule,必须重写render方法
class Uimodule(UIModule):
    def render(self, *args, **kwargs):
        return '我是ui_module'
class Advertisement(UIModule):
    def render(self, *args, **kwargs):
        return self.render_string('07ad.html')
    def css_files(self):
        return "css/King_Chance_Layer7.css"
    def javascript_files(self):
        return [
#注意,jquery在最前
            "js/jquery_1_7.js",
            "js/King_Chance_Layer.js",

            "js/King_Layer_test.js",

        ]
## ui_methods.py
'''
this is method
'''

def ui_method1(self):
    return 'ui_method1'

def ui_method2(self):
    return 'ui_method2'
## 07module.html
<!DOCTYPE html>
<br lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板</title>
</head>
<br>
{{ username }}</br>
{{ fun() }}</br>

{{ calc() }}<br>
{{ calc().sum(6,9) }}</br>

{% import time %}
{{ time.time() }}<br>

{% from util.module_file import sub,upper,Count %}

{{ sub(5,3) }}</br>
{{ upper('abcd') }}</br>

{{ Count().url}}</br>
{{ Count().sum(6,9) }}</br>
{{ Count.sum(6,9) }}</br>

{{ ui_method1() }}</br>
666 </br>
{% module Uimodule()  %}</br>
666
{% module Advertisement()  %}</br>

set设置局部变量
{% set su=Count().sum %}
{{ su(6,9) }}</br>

{% apply upper %}
hao e </br>
bu e
{% end %}
</br>

{{ linkify('百度:http://www.baidu.com') }}</br>
{% raw linkify('百度:http://www.baidu.com') %}</br>



</body>
</html>
原文地址:https://www.cnblogs.com/lajiao/p/7749900.html