Mik*_*ers 6 python haml preprocessor mako
我想以某种方式设备mako.lookup.TemplateLookup,以便它仅为某些文件扩展名应用某些预处理器.
具体来说,我有一个haml.preprocessor我想应用于文件名结尾的所有模板.haml.
谢谢!
您应该能够自定义 TemplateLookup 以获得您想要的行为。
自定义查找.py
from mako.lookup import TemplateLookup
import haml
class Lookup(TemplateLookup):
def get_template(self, uri):
if uri.rsplit('.')[1] == 'haml':
# change preprocessor used for this template
default = self.template_args['preprocessor']
self.template_args['preprocessor'] = haml.preprocessor
template = super(Lookup, self).get_template(uri)
# change it back
self.template_args['preprocessor'] = default
else:
template = super(Lookup, self).get_template(uri)
return template
lookup = Lookup(['.'])
print lookup.get_template('index.haml').render()
Run Code Online (Sandbox Code Playgroud)
索引.haml
<%inherit file="base.html"/>
<%block name="content">
%h1 Hello
</%block>
Run Code Online (Sandbox Code Playgroud)
基本.html
<html>
<body>
<%block name="content"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)