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

标签 python preprocessor haml mako

我想以某种方式检测 mako.lookup.TemplateLookup,以便它仅对某些文件扩展名应用某些预处理器。

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

谢谢!

最佳答案

您应该能够自定义 TemplateLookup 以获得您想要的行为。

customlookup.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()

index.haml

<%inherit file="base.html"/>

<%block name="content">
  %h1 Hello
</%block>

base.html

<html>
  <body>
    <%block name="content"/>
  </body>
</html>

关于python - 根据文件扩展名选择 Mako 预处理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8300752/

相关文章:

c++ - 是否有一种代码预处理工具可以将多个用户定义的包含(无系统包含)合并到一个文件中?

angularjs - 如何在 Bootstrap 中有条件地控制/停止数据切换?

ruby-on-rails - Rails3.1 引擎 : can't get SLIM or HAML to work in test/dummy app

python - Pyglet - 游戏运行缓慢,对事件和东西感到困惑

python - 寻找解决 pygame 程序中逻辑错误的方法

python - 如何将项目添加到 collection.Counter?然后将它们分类为 ASC?

相当于 '#define func() ' 的 python 或如何在 python 中注释掉函数调用

haml - 仅在 Haml 中创建局部变量

python - WebDriver等待 javascript 值

python - 如何使用 arcGIS api 获取两个地址之间的多个行驶时间