python全栈开发

tornado 4.5.2 配置模板

2017-12-05  本文已影响5人  LionelDong

tornado 4.5.2

配置模板

def get_template_path(self):
    """Override to customize template path for each handler.

    By default, we use the ``template_path`` application setting.
    Return None to load templates relative to the calling file.
    """
    return self.application.settings.get("template_path")
    def render_string(self, template_name, **kwargs):
        """Generate the given template with the given arguments.

        We return the generated byte string (in utf8). To generate and
        write a template as a response, use render() above.
        """
        # If no template_path is specified, use the path of the calling file
        template_path = self.get_template_path()
        
        """
        如下代码表明没找到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)
        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]
        t = loader.load(template_name)
        namespace = self.get_template_namespace()
        namespace.update(kwargs)
        return t.generate(**namespace)
上一篇 下一篇

猜你喜欢

热点阅读