Use Tornado
to achieve a simple task list feature demo to explain tornado framework template engine
A, demo directory structure
Second, the specific content of the document
2.1、commons.css
.body{ margin: 0; background-color: bisque; }
2.2、index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- <link rel="stylesheet" href="../static/commons.css"/>--> <link rel="stylesheet" href='{{static_url("commons.css")}}'/> </head> <body> <h1>{{ag}}</h1> <h2>{{test_uimethod()}}</h2> <h3>{% module MyClass() %}</h3> <form method="post"> <input type="text" name="name"/> <input type="submit" value="提交"/> </form> <h1>显示内容</h1> <ul> {% for item in contents %} <li>{{item}}</li> {% end %} </ul> </body> </html>
2.3、uimodule.py
#!/usr/bin/env python # -*- coding: utf-8 -*- from tornado.web import UIModule class MyClass(UIModule): def render(self, *args, **kwargs): return 'UIModule'
2.4、uimethod.py
#!/usr/bin/env python # -*- coding: utf-8 -*- def test_uimethod(self): return 'uimethod'
2.5、index.py
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import tornado.web 4 import tornado.ioloop 5 import uimethod as ut 6 import uimodule as ud 7 8 class IndexHandle(tornado.web.RequestHandler): 9 def get(self, *args, **kwargs): 10 self.render('index.html', contents=CONTENTS_LIST, ag="") 11 12 def post(self, *args, **kwargs): 13 CONTENTS_LIST.append (self.get_argument ( ' name ' )) 14 self.render ( ' index.html ' , = CONTENTS_LIST Contents, AG = ' the this IS AG ' ) 15 16 IF the __name__ == ' __main__ ' : . 17 CONTENTS_LIST = [ ] # for storing the contents of the input box 18 # dictionary represents the profile . 19 Settings = { 20 is ' template_path ' : ' Template ' , #Template file storage location 21 ' static_path ' : ' static ' , # store static files 22 ' static_url_prefix ' : ' static / ' , # static file prefix, to reduce the introduction of each file must be prefixed trouble 23 ' ui_methods ' : UT, 24 ' ui_modules ' : UD, 25 } 26 is 27 file application = tornado.web.Application ([ 28 (R & lt ' / index ' , IndexHandle) 29 ], ** Settings) 30 application.listen (80) # Set the server listening port 31 tornado.ioloop.IOLoop.instance (). Start () # blocking the server process, waiting for client access
2.6, demo operation renderings
Detailed demo:
- Template engine
{{key}}
represents takingkey
a value corresponding to, whenkey
executing the function is a function of time and taking the function result. For exampleindex.html
the file<h1>{{ag}}</h1>
is actually acquired isindex.py
theself.render("index.html", ag="this is ag", contents=CONTENTS_LIST)
parameterag
values <h1>{{test_uimethod()}}</h1>
Here is the implementation of a custom function, the custom function we will write in theuimethod.py
file, andindex.py
import file, then theindex.py
file issettings
configured to add a line'ui_methods': ut
, the line represents the contents of the template engine can perform custom functions- Template engine
{%%}
can be used to loop and conditional language as well as self-executing custom class,{% for item in contents %}
where it is used to loop throughcontents
content <h1>{%module MyClass()%}</h1>
Represented here to perform a custom template engine class that the file corresponds to theuimodule.py
file, we needindex.py
thesettings
increase in a row'ui_modules': ud
, diverted to represent the template engine can use a custom class- 注意, 我们将
index.html
文件引入css
的方式改为了<link rel="stylesheet" href='{{static_url("commons.css")}}'>
,static_url()
是模板引擎内置的自定义函数, 用该函数引入css
文件时候, 仅当css
文件内容发生变化时候, 浏览器才会重新缓存该css
文件