您的位置:首页 > 其它

tornado 学习笔记10 Web应用中模板(Template)的工作流程分析

2015-11-13 17:09 671 查看
         第8,9节中,我们分析Tornado模板系统的语法、使用以及源代码中涉及到的相关类,而且对相关的源代码进行了分析。那么,在一个真正的Web应用程序中,模板到底是怎样使用?怎样被渲染?工作流程到底如何?

10.1 工作流程

 





 

10.2 几个关键值

(1) template-path 模板路径的确定。

     先看源代码(位于web.py文件中RequestHandler类的render_string 函数中):

template_path = self.get_template_path()
if not template_path:
frame = sys._getframe(0)
web_file = frame.f_code.co_filename
while frame.f_code.co_filename == web_file:
frame = frame.f_back
template_path = os.path.dirname(frame.f_code.co_filename)


      从源代码中可以获知,template_path模板路径的确定顺序如下:

首先,从Application的设置属性中的template_path选项中获取。

其次,当前请求处理handler文件所处的目录就是模板的路径。所以,如果你没有配置template_path选项,则模板文件必须与handler位于同一个目录下。

 

(2) 模板加载器(loader)的确定

        先看源代码:

with RequestHandler._template_loader_lock:
if template_path not in RequestHandler._template_loaders:
loader = self.create_template_loader(template_path)
RequestHandler._template_loaders[template_path] = loader
else:
loader = RequestHandler._template_loaders[template_path]


       从源代码中我们首先可以看出,模板加载器在整个Web应用程序中是单例的。而后,我们再进入create_template_loader方法中,由这个方法最终确定loader。

def create_template_loader(self, template_path):
"""Returns a new template loader for the given path.

May be overridden by subclasses.  By default returns a
directory-based loader on the given path, using the
``autoescape`` application setting.  If a ``template_loader``
application setting is supplied, uses that instead.
"""
settings = self.application.settings
if "template_loader" in settings:
return settings["template_loader"]
kwargs = {}
if "autoescape" in settings:
# autoescape=None means "no escaping", so we have to be sure
# to only pass this kwarg if the user asked for it.
kwargs["autoescape"] = settings["autoescape"]
return template.Loader(template_path, **kwargs)


        同样,根据create_template_loader方法的实现代码,我们可以得知loader的确定过程如下:

首先,从Application的设置属性中的template_loader选项中获取。

其次,使用Tornado默认的模板加载器,也就是第9节讲到的tornado.template.Loader

       所以,如果不想使用内置的模板引擎的话,可以定义自己的loader(比如说jinja模板引擎),然后设置application的template_loader选项的值。

 

(3) 模板中自定义python方法的确定

      在第8节中,我们知道,Tornado给我们内置了一些方法。但是如何使用自定义的方法呢?两种方式可以实现:

一、通过重写RequestHandler的get_template_namespace来确定;

二、配置Application 设置中的ui_methods选项。

     两者的区别在于,第一种是作用范围是针对每一个handler,而第二种是全局的配置,适用于所有的handler.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: