Python Mako Template 学习笔记[转]
来源:互联网 发布:淘宝托管公司排名 编辑:程序博客网 时间:2024/04/30 00:07
Mako是什么?Moko是Python写的一个模板库,Python官网python.org用的就是它哦。其他废话也就不累赘了,直接来点代码,方便阅读与了解把。
(Mako官网地址:http://www.makotemplates.org/ ,可以下载安装包,推荐使用easy_install安装)
from mako.template import Template
mytemplate = Template("hello world!")
print mytemplate.render()
mytemplate = Template("hello, ${name}!")
print mytemplate.render(name="jack")
代码可以参考官方doc部分
mytemplate = Template(filename='/docs/mytmpl.txt')
print mytemplate.render()
还可以从设置模板为文件,设置filename属性
mytemplate = Template(filename='/docs/mytmpl.txt', module_directory='/tmp/mako_modules')
print mytemplate.render()
文件还可以缓存到某个目录下,下面的/docs/mytmpl.txt会产生一个py:/tmp/mako_modules/docs/mytmpl.txt.py
from mako.lookup import TemplateLookup
mylookup = TemplateLookup(directories=['/docs'])
mytemplate = Template("""<%include file="header.txt"/> hello world!""", lookup=mylookup)
查找模板,方便统一模板路径使用。
mylookup = TemplateLookup(directories=['/docs'], module_directory='/tmp/mako_modules')
def serve_template(templatename, **kwargs):
mytemplate = mylookup.get_template(templatename)
print mytemplate.render(**kwargs)
改良了上面的查找方式
mylookup = TemplateLookup(directories=['/docs'], output_encoding='utf-8',
encoding_errors='replace')
mytemplate = mylookup.get_template("foo.txt")
print mytemplate.render()
设置输出编码,以及编码错误时候处理方式
转载来源:"小鱼博客" http://chenxiaoyu.org/blog/
-End-
- Python Mako Template 学习笔记[转]
- python template mako 学习笔记
- Python Mako Template 学习笔记
- Python Mako笔记
- mako模板学习笔记
- mako模板学习笔记
- Python Mako
- Python Mako语法
- mako模板笔记
- [笔记]python template 模板
- class template学习笔记
- template学习笔记
- C++ TEMPLATE 学习笔记
- jade template 学习笔记
- C++ template 学习笔记
- Go Template学习笔记
- Python模板库Mako的语法
- Python模板库Mako的语法
- sicily 1240 Faulty Odometer
- ffmpeg tutorial 01----08讲解分析博文 + GStreamer的使用
- __DATE__,__FILE__,__LINE__,__TIME__,__FUNCTION__
- log
- ubuntu 下配置grails
- Python Mako Template 学习笔记[转]
- Gaussian envelope
- 路由器与交换机的功能和区别
- 周中小结
- [Mako]中文使用体验【limodou】
- sicily 1780 Faulty Odometer Again
- 二维数组 及 转换为 字符串
- 交换机、集线器、路由器区别和使用
- 把字符串"1,4,1000,34"按數字方式排序