Mako
一、安装
1.安装setup tools
在网站https://pypi.python.org/pypi/setuptools/ 下载setuptools-3.4.1.tar.gz
tar xvf setuptools-3.4.1.tar.gz
cd setuptools-3.4.1
sudo python setup.py install
2.安装mako
$wget https://pypi.python.org/packages/source/M/Mako/Mako-0.9.1.tar.gz
$tar xvf Mako-0.9.1.tar.gz
$cd Mako-0.9.1
$sudo python setup.py install
二、使用
1.basic usage(hello,world)
rom mako.template import Template
mytemplate = Template("hello, ${name}!")
print(mytemplate.render(name="world"))
2.Using File-Based Templates
from mako.template import Template
mytemplate = Template(filename='/docs/mytmpl.txt', module_directory='/tmp/mako_modules')
print(mytemplate.render())
3.TemplateLookup
from mako.template import Template
from mako.lookup import TemplateLookup
mylookup = TemplateLookup(directories=['/docs'], module_directory='/tmp/mako_modules')
def serve_template(templatename, **kwargs):
mytemplate = mylookup.get_template(templatename)
print(mytemplate.render(**kwargs))
网友评论