Python模板库Mako的用法
admin
2023-07-31 00:47:31
0

Mako是一个高性能的Python模板库,它的语法和API借鉴了很多其他的模板库,如Django、Jinja2等等。

基本用法

创建模板并渲染它的最基本的方法是使用 Template 类:

12345 from mako.template import Template t = Template(\’hello world!\’) print t.render()

传给 Template 的文本参数被编译为一个Python模块。模块包含一个 render_body() 函数,它产生模板的输出。调用 render() 方法时,Mako建立了一个模板的运行环境,并调用 render_body() 函数,把输出保存到缓冲,返回它的字符串内容。
render_body() 函数中可以访问一个变量集。可以向 render() 方法发送额外的关键词参数来指定这些变量:

12345 from mako.template import Template t = Template(\’hello, ${name}!\’) print t.render(name=\’yeolar\’)

ender() 方法使Mako创建一个 Context 对象,它存储模板可以访问的所有变量和一个用来保存输出的缓冲。也可以自己创建 Context ,用 render_context() 方法使模板用它来渲染:

123456789101112131415 from mako.template import Template from mako.runtime import Context from StringIO import StringIO t = Template(\’hello, ${name}!\’) buf = StringIO() c = Context(buf, name=\’yeolar\’) t.render_context(c) print buf.getValue()

使用文件模板

Template 也可以从文件加载模板,使用 filename 参数:

12345 from mako.template import Template t = Template(filename=\’/docs/tpl.txt\’) print t.render()

为了提高性能,从文件加载的 Template 还可以在文件系统中将生成的模块缓存为一般的Python模块文件(.py文件),这通过添加 module_directory 参数实现:

12345 from mako.template import Template t = Template(filename=\’/docs/tpl.txt\’, module_directory=\’/tmp/mako_modules\’) print t.render()

上面的代码渲染后,会创建一个/tmp/mako_modules/docs/tpl.txt.py文件,其中包含模块的源代码。下次同样参数的 Template 创建时,自动重用这个模块文件。

使用TemplateLookup

到现在的例子都是有关单个 Template 对象的用法。如果模板中的代码要定位其他模板资源,需要某种使用URI来找到它们的方法。这种需求是由 TemplateLookup 类来达到的。这个类通过传入一个模板查找目录的列表来构造,然后作为关键词参数传给 Template 对象:

1234567 from mako.template import Template from mako.lookup import TemplateLookup lookup = TemplateLookup(directories=[\’/docs\’]) t = Template(\'<%include file=\”header.txt\” /> hello word!\’, lookup=lookup)

上面创建的模板中包含文件header.txt。为了查找header.txt,传了一个 TemplateLookup 对象给它。
通常,应用会以文本文件形式在文件系统上存储大部分或全部的模板。一个真正的应用会直接从 TemplateLookup 取得它的模板,使用 get_template() 方法,它接受需要的模板的URI作为参数:

1234567891011 from mako.template import Template from mako.lookup import TemplateLookup lookup = TemplateLookup(directories=[\’/docs\’] id=\”crayon-581284a88d073345049979-4\”> lookup = TemplateLookup(directories=[\’/docs\’]es notranslate\” data-settings=\” minimize scroll-always\” style=\” margin-top: 12px; margin-bottom: 12px; font-size: 13px !important; line-height: 15px !important;\”>

12345 from mako.template import Template t = Template(\’hello world!\’) print t.render()


传给 Template 的文本参数被编译为一个Python模块。模块包含一个 render_body() 函数,它产生模板的输出。调用 render() 方法时,Mako建立了一个模板的运行环境,并调用 render_body() 函数,把输出保存到缓冲,返回它的字符串内容。
render_body() 函数中可以访问一个变量集。可以向 render() 方法发送额外的关键词参数来指定这些变量:

12345 from mako.template import Template t = Template(\’hello, ${name}!\’) print t.render(name=\’yeolar\’)

ender() 方法使Mako创建一个 Context 对象,它存储模板可以访问的所有变量和一个用来保存输出的缓冲。也可以自己创建 Context ,用 render_context() 方法使模板用它来渲染:

123456789101112131415 from mako.template import Template from mako.runtime import Context from StringIO import StringIO t = Template(\’hello, ${name}!\’) buf = StringIO() c = Context(buf, name=\’yeolar\’) t.render_context(c) print buf.getValue()

使用文件模板

Template 也可以从文件加载模板,使用 filename 参数:

12345 from mako.template import Template t = Template(filename=\’/docs/tpl.txt\’) print t.render()

为了提高性能,从文件加载的 Template 还可以在文件系统中将生成的模块缓存为一般的Python模块文件(.py文件),这通过添加 module_directory 参数实现:

12345 from mako.template import Template t = Template(filename=\’/docs/tpl.txt\’, module_directory=\’/tmp/mako_modules\’) print t.render()

上面的代码渲染后,会创建一个/tmp/mako_modules/docs/tpl.txt.py文件,其中包含模块的源代码。下次同样参数的 Template 创建时,自动重用这个模块文件。

使用TemplateLookup

到现在的例子都是有关单个 Template 对象的用法。如果模板中的代码要定位其他模板资源,需要某种使用URI来找到它们的方法。这种需求是由 TemplateLookup 类来达到的。这个类通过传入一个模板查找目录的列表来构造,然后作为关键词参数传给 Template 对象:

1234567 from mako.template import Template from mako.lookup import TemplateLookup lookup = TemplateLookup(directories=[\’/docs\’]) t = Template(\'<%include file=\”header.txt\” /> hello word!\’, lookup=lookup)

上面创建的模板中包含文件header.txt。为了查找header.txt,传了一个 TemplateLookup 对象给它。
通常,应用会以文本文件形式在文件系统上存储大部分或全部的模板。一个真正的应用会直接从 TemplateLookup 取得它的模板,使用 get_template() 方法,它接受需要的模板的URI作为参数:

1234567891011 from mako.template import Template from mako.lookup import TemplateLookup lookup = TemplateLookup(directories=[\’/docs\’]4a88d067192763136-13\”>131415 from mako

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python绘图库Matplo... 本文简单介绍了Python绘图库Matplotlib的安装,简介如下: matplotlib是pyt...
Prometheus+Graf... 一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在Sound...