import tornado.httpserver
import tornado.ioloop
import tornado.web
# define one "add" customization funcation which will be used in the assigned template file.
def add(x, y):
return (x+y)
class MainHandler(tornado.web.RequestHandler):
def get(self):
items = ["item1","item2","item3"]
# render the corresponding template file, and pass the "**args" to the assigned template
# not only we can pass the realted parameters, but also can pass the related functions.
# so extendible and powerful! :)
self.render("templates/template_test.html", items=items, add=add)
compile(source, filename, mode[, flags[, dont_inherit]])
compile(source, filename ,mode)函数将一个字符串编译为字节代码, source是将要被编译的字符串, filename是定义该字符串变量的文件(示例代码中的temp将作为一个变量,用于将被执行的语句中,即str),mode参数指定了代码被编译的类型
'exec' if source consists of a sequence of statements
'eval' if it consists of a single expression
'single' if it consists of a single interactive statement
compile()函数返回一个代码对象,该对象当然也可以被传递给eval()函数和exec语句来执行
小结:
template.py 很粗略的看了下,越发觉得tornado很值得学习 ;)
请大家帮忙予以指正、帮助啊,如果有什么地方我理解错了或者好的建议等等!