根据文件扩展名选择Mako预处理器?

2024-05-06 20:47:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想以某种方式插入mako.lookup.TemplateLookup,这样它只对某些文件扩展名应用某些预处理程序。在

具体地说,我有一个haml.preprocessor,我想将其应用于文件名以.haml结尾的所有模板。在

谢谢!在


Tags: 文件程序模板文件名mako结尾方式lookup
1条回答
网友
1楼 · 发布于 2024-05-06 20:47:36

您应该能够定制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()

索引.haml

^{pr2}$

基本.html

^{3}$

相关问题 更多 >