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


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

    10.1 工作流程

    image

    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.

  • 相关阅读:
    类型约束的本质:泛型是不完备类型,只有合乎要求的构造才能正确使用和访问。
    函数的泛型约束是函数签名的一部分,不符合约束的初始调用将不能查找到函数(报错)
    泛型约束-swift
    swift语言的特点(相对于oc)
    extension Kingfisher where Base: Image:泛型类型的具体化与实例化
    “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法
    int 和bigint差别有多大?
    MySql5.7 配置文件 my.cnf 设置
    关于mysql 出现 1264 Out of range value for column 错误的解决办法
    git中Please enter a commit message to explain why this merge is necessary.
  • 原文地址:https://www.cnblogs.com/liaofeifight/p/4962660.html
Copyright © 2020-2023  润新知