让我们一起来构建一个模板引擎(三)
admin
2023-07-31 00:44:45
0

在 上篇文章 中我们的模板引擎实现了对 iffor 对支持,同时在文章的最后我给大家留了一个 问题:如何实现支持 includeextends 的标签功能。

在本篇文章中我们将一起来动手实现这两个功能。

include

include 标签对语法是这样的:假设有一个 item.html 模板文件,它的内容如下:

1
  • {{ item }}
  • 还有一个我们要渲染的模板 list.html 内容如下:

    12345
        {% for item in items %}    {% include \”item.html\” %}  {% endfor %}

    渲染 list.html 后的结果类似:

    12345
        
    • item1
    •   
    • item2
    •   
    • item3

    从上面可以看出来 include 标签的作用类似使用 include 所在位置的名字空间 渲染另一个模板然后再使用渲染后的结果。所以我们可以将 include 的模板文件 当作普通的模板文件来处理,用解析那个模板生成后的代码替换 include 所在的位置, 再将结果追加到 result_var 。 生成的代码类似:

    1234567891011 def func_name():    result = []     # 解析 include 的模板    def func_name_include():        result_include = []        return \’\’.join(result_include)    # 调用生成的 func_name_include 函数获取渲染结果    result.append(func_name_include())     return \’\’.join(result)

    生成类似上面的代码就是 include 的关键点,下面看一下实现 include 功能 都做了哪些改动 (可以从 Github 上下载 template3a.py):

    123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 class Template:     def __init__(self, ..., template_dir=\’\’, encoding=\’utf-8\’):        # ...        self.template_dir = template_dir        self.encoding = encoding        # ...     def _handle_tag(self, token):        \”\”\”处理标签\”\”\”        # ...        tag_name = tag.split()[0]        if tag_name == \’include\’:            self._handle_include(tag)        else:            self._handle_statement(tag)     def _handle_include(self, tag):        filename = tag.split()[1].strip(\’\”\\\’\’)        included_template = self._parse_another_template_file(filename)        # 把解析 include 模板后得到的代码加入当前代码中        # def __func_name():        #    __result = []class=\”crayon-o\”>:        #    __result = []实现支持 includeextends 的标签功能。

    在本篇文章中我们将一起来动手实现这两个功能。

    include

    include 标签对语法是这样的:假设有一个 item.html 模板文件,它的内容如下:

    1
  • {{ item }}
  • 还有一个我们要渲染的模板 list.html 内容如下:

    12345
        {% for item in items %}    {% include \”item.html\” %}  {% endfor %}

    渲染 list.html 后的结果类似:

    12345
        
    • item1
    •   
    • item2
    •   
    • item3

    从上面可以看出来 include 标签的作用类似使用 include 所在位置的名字空间 渲染另一个模板然后再使用渲染后的结果。所以我们可以将 include 的模板文件 当作普通的模板文件来处理,用解析那个模板生成后的代码替换 include 所在的位置, 再将结果追加到 result_var 。 生成的代码类似:

    1234567891011 def func_name():    result = []     # 解析 include 的模板    def func_name_include():        result_include = []        return \’\’.join(result_include)    # 调用生成的 func_name_include 函数获取渲染结果    result.append(func_name_include())     return \’\’.join(result)

    生成类似上面的代码就是 include 的关键点,下面看一下实现 include 功能 都做了哪些改动 (可以从 Github 上下载 template3a.py):

    123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 class Template:     def __init__(self, ..., template_dir=\’\’, encoding=\’utf-8\’):        # ...        self.template_dir = template_dir        self.encoding = encoding        # ...     def _handle_tag(self, token):        \”\”\”处理标签\”\”\”        # ...        tag_name = tag.split()[0]        if tag_name == \’include\’:            self._handle_include(tag)        else:            self._handle_statement(tag)     def _handle_include(self, tag):        filename = tag.split()[1].strip(\’\”\\\’\’)        included_template = self._parse_another_template_file(filename)        # 把解析 include 模板后得到的代码加入当前代码中        # def __func_name():        #    __result = []

    相关内容

    热门资讯

    Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
    定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
    scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
    500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
    小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
    pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
    微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
    65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
    Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
    项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...