模板语言就是可以在html页面,接收逻辑处理的self.render()方法传输的变量,将数据渲染到对应的地方
一、接收值渲染
{{...}}接收self.render()方法传值的变量或一个值
#!/usr/bin/env python
#coding:utf-8
import tornado.ioloop
import tornado.web #导入tornado模块下的web文件
#逻辑处理
class MainHandler(tornado.web.RequestHandler): #定义一个类,继承tornado.web下的RequestHandler类
def get(self): #get()方法,接收get方式请求
self.render("cshi.html",zhi = "网站名称") #显示cshi.html文件,将zhi变量传到模板语言里渲染
settings = { #html文件归类配置,设置一个字典
"template_path":"template", #键为template_path固定的,值为要存放HTML的文件夹名称
"static_path":"static", #键为static_path固定的,值为要存放js和css的文件夹名称
}
#路由映射
application = tornado.web.Application([ #创建一个变量等于tornado.web下的Application方法
(r"/index", MainHandler), #判断用户请求路径后缀是否匹配字符串index,如果匹配执行MainHandler方法
],**settings) #将html文件归类配置字典,写在路由映射的第二个参数里
if __name__ == "__main__":
#内部socket运行起来
application.listen(8888) #设置端口
tornado.ioloop.IOLoop.instance().start()
html


二、接收值,运行代码块
{%...%}{%end%}在html渲染代码块,支持html里代码块书写,并能接收self.render()方法传值
#!/usr/bin/env python
#coding:utf-8
import tornado.ioloop
import tornado.web #导入tornado模块下的web文件
#逻辑处理
class MainHandler(tornado.web.RequestHandler): #定义一个类,继承tornado.web下的RequestHandler类
def get(self): #get()方法,接收get方式请求
self.render("cshi.html",zhi = ["首页","资讯","关于"]) #显示cshi.html文件,将列表传到模板语言里渲染
settings = { #html文件归类配置,设置一个字典
"template_path":"template", #键为template_path固定的,值为要存放HTML的文件夹名称
"static_path":"static", #键为static_path固定的,值为要存放js和css的文件夹名称
}
#路由映射
application = tornado.web.Application([ #创建一个变量等于tornado.web下的Application方法
(r"/index", MainHandler), #判断用户请求路径后缀是否匹配字符串index,如果匹配执行MainHandler方法
],**settings) #将html文件归类配置字典,写在路由映射的第二个参数里
if __name__ == "__main__":
#内部socket运行起来
application.listen(8888) #设置端口
tornado.ioloop.IOLoop.instance().start()
html


模板语言,for循环列表加if判断
#!/usr/bin/env python
#coding:utf-8
import tornado.ioloop
import tornado.web #导入tornado模块下的web文件
#逻辑处理
class MainHandler(tornado.web.RequestHandler): #定义一个类,继承tornado.web下的RequestHandler类
def get(self): #get()方法,接收get方式请求
self.render("cshi.html",zhi = ["首页","资讯","关于"]) #显示cshi.html文件,将列表传到模板语言里渲染
settings = { #html文件归类配置,设置一个字典
"template_path":"template", #键为template_path固定的,值为要存放HTML的文件夹名称
"static_path":"static", #键为static_path固定的,值为要存放js和css的文件夹名称
}
#路由映射
application = tornado.web.Application([ #创建一个变量等于tornado.web下的Application方法
(r"/index", MainHandler), #判断用户请求路径后缀是否匹配字符串index,如果匹配执行MainHandler方法
],**settings) #将html文件归类配置字典,写在路由映射的第二个参数里
if __name__ == "__main__":
#内部socket运行起来
application.listen(8888) #设置端口
tornado.ioloop.IOLoop.instance().start()
html


三、自定义,可以在html里执行函数
有两种方式,一种叫uimethod,一种叫uimodule
uimethod方式
1、首先新建一个py文件来写html可以调用的函数
2、在模板引擎导入新建的py文件,在逻辑处理配置字典里配置键为ui_methods(固定的):值为新建的py文件路径名称
3、在html{{func()}}执行py文件里的函数
模板引擎
#!/usr/bin/env python
#coding:utf-8
import tornado.ioloop
import tornado.web #导入tornado模块下的web文件
import uimethod
#逻辑处理
class MainHandler(tornado.web.RequestHandler): #定义一个类,继承tornado.web下的RequestHandler类
def get(self): #get()方法,接收get方式请求
self.render("cshi.html",zhi = ["首页","资讯","关于"]) #显示cshi.html文件,将列表传到模板语言里渲染
settings = { #html文件归类配置,设置一个字典
"template_path":"template", #键为template_path固定的,值为要存放HTML的文件夹名称
"static_path":"static", #键为static_path固定的,值为要存放js和css的文件夹名称
'ui_methods': uimethod, #配置html文件函数调用模块
}
#路由映射
application = tornado.web.Application([ #创建一个变量等于tornado.web下的Application方法
(r"/index", MainHandler), #判断用户请求路径后缀是否匹配字符串index,如果匹配执行MainHandler方法
],**settings) #将html文件归类配置字典,写在路由映射的第二个参数里
if __name__ == "__main__":
#内部socket运行起来
application.listen(8888) #设置端口
tornado.ioloop.IOLoop.instance().start()
uimethod.py文件,html函数调用文件
#!/usr/bin/env python
#coding:utf-8
def func(self):
return"123"
html


uimodule方式
1、新建一个py文件来写html文件调用函数,这个py文件必须导入tornado.web下的UIModule方法,在py文件创建类必须继承UIModule方法,在这个类里写html执行函数
2、模板引擎导入新建的py文件,在模板引擎配置字典配置访问,ui_modules固定的:uimodule新建py文件路径名称,
3、在html调用py文件里的函数{%modulefghj() %},注意调用方式{%module固定的fghj()要执行的函数%}
模板引擎
#!/usr/bin/env python
#coding:utf-8
import tornado.ioloop
import tornado.web #导入tornado模块下的web文件
import uimodule
#逻辑处理
class MainHandler(tornado.web.RequestHandler): #定义一个类,继承tornado.web下的RequestHandler类
def get(self): #get()方法,接收get方式请求
self.render("cshi.html",zhi = ["首页","资讯","关于"]) #显示cshi.html文件,将列表传到模板语言里渲染
settings = { #html文件归类配置,设置一个字典
"template_path":"template", #键为template_path固定的,值为要存放HTML的文件夹名称
"static_path":"static", #键为static_path固定的,值为要存放js和css的文件夹名称
'ui_modules':uimodule, #配置html文件函数调用模块
}
#路由映射
application = tornado.web.Application([ #创建一个变量等于tornado.web下的Application方法
(r"/index", MainHandler), #判断用户请求路径后缀是否匹配字符串index,如果匹配执行MainHandler方法
],**settings) #将html文件归类配置字典,写在路由映射的第二个参数里
if __name__ == "__main__":
#内部socket运行起来
application.listen(8888) #设置端口
tornado.ioloop.IOLoop.instance().start()
uimodule.py文件,html函数调用文件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tornado.web import UIModule
from tornado import escape
class custom(UIModule):
def render(self, *args, **kwargs):
return"123"
class fghj(UIModule):
def render(self, *args, **kwargs):
return[1,2,3,4,5]
html


不知道为什么html模板代码不能复制,只好放上截图了
网友评论