Choose a Mako preprocessor based on file extension?

I would like to somehow measure mako.lookup.TemplateLookup so that it uses some preprocessors only for specific file extensions.

In particular, I have haml.preprocessor which I would like to apply to all templates whose file name ends with .haml .

Thanks!

+6
source share
1 answer

You should be able to customize TemplateLookup to get the desired behavior.

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> 
+4
source

Source: https://habr.com/ru/post/902585/


All Articles