diff options
author | Ben Bangert <ben@groovie.org> | 2006-12-08 02:16:34 +0000 |
---|---|---|
committer | Ben Bangert <ben@groovie.org> | 2006-12-08 02:16:34 +0000 |
commit | c4292864f7eb24d2d4ba418f21ebd3a3b28604d9 (patch) | |
tree | 784fd94eaa560c4a917b1086c7ad6e76033bc78c | |
parent | d15de1d50064a0e057bce4617bed488429aec148 (diff) | |
download | mako-c4292864f7eb24d2d4ba418f21ebd3a3b28604d9.tar.gz |
Adding Mako TurboGears plugin support.
-rw-r--r-- | lib/mako/plugin.py | 39 | ||||
-rw-r--r-- | setup.py | 4 |
2 files changed, 43 insertions, 0 deletions
diff --git a/lib/mako/plugin.py b/lib/mako/plugin.py new file mode 100644 index 0000000..d579eed --- /dev/null +++ b/lib/mako/plugin.py @@ -0,0 +1,39 @@ +import re +from mako.lookup import TemplateLookup + +class TGPlugin(object): + """TurboGears compatible Template Plugin.""" + extension = 'mak' + + def __init__(self, extra_vars_func=None, options=None): + self.extra_vars_func = extra_vars_func + if not options: + options = {} + + # Pull the options out and initialize the lookup + tmpl_options = {} + for k, v in options.iteritems(): + if k.startswith('mako.'): + tmpl_options[k[5:]] = v + self.lookup = TemplateLookup(**tmpl_options) + + def load_template(self, templatename): + """Unused method for TG plug-in API compat""" + pass + + def render(self, info, format="html", fragment=False, template=None): + # Translate TG dot notation to normal / template path + if '/' and '.' not in template: + template = '/' + template.replace('.', '/') + '.' + self.extension + + if not template.startswith('/'): + template = '/' + template + + # Load extra vars func if provided + if self.extra_vars_func: + info.update(self.extra_vars_func()) + + # Lookup template + template = self.lookup.get_template(template) + return template.render(**info) + @@ -37,4 +37,8 @@ The latest version is available in a `Subversion repository install_requires=[ 'MyghtyUtils', ], + entry_points=""" + [python.templating.engines] + mako = mako.plugin:TGPlugin + """, ) |