Skip to content

Instantly share code, notes, and snippets.

@alxf
Created January 20, 2012 11:45
Show Gist options
  • Save alxf/1646995 to your computer and use it in GitHub Desktop.
Save alxf/1646995 to your computer and use it in GitHub Desktop.
A rewrite of render_mako class from web.contrib.template (Web.py)
#webpy with mako
class render_mako(object):
"""Rendering interface to Mako Templates.
Example:
render = render_mako(
directories=['templates'],
input_encoding='utf-8',
output_encoding='utf-8',
extension='.mako'
)
render.hello()
The rendered template will be in this example: templates/hello.mako
"""
def __init__(self, *a, **kwargs):
#store custom extension
if kwargs.has_key('extension'):
self._ext = kwargs['extension']
del kwargs['extension']
#otherwise set it to .html
else:
self._ext = '.html'
#proceed to normal execution
from mako.lookup import TemplateLookup
self._lookup = TemplateLookup(*a, **kwargs)
def __getattr__(self, name):
path = name + self._ext
t = self._lookup.get_template(path)
return t.render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment