# makodecorator.py
# License: None, public domain
# Version: 1.0.0
# Author: Robert Thomson - http://blog.corporatism.org/
from mako.lookup import TemplateLookup
from mako import exceptions
import settings
import sys
import os.path
from django.http import HttpResponse
# with thanks to Peter Hunt (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/465427)
decorator_with_args = lambda decorator: lambda *args, **kwargs: lambda func: \
decorator(func, *args, **kwargs)
@decorator_with_args
def mako(func, template=None):
def MAKOIFY(request, *args, **kwargs):
res = func(request, *args, **kwargs)
if not res:
res = {}
if type(res) == dict:
# use current_app/mako_templates/ as first search path.
# the assumption is that the function's module is under current_app/
d = os.path.join(
os.path.dirname(sys.modules[func.__module__].__file__),
"mako_templates/")
lookup = TemplateLookup(directories=[d, 'mako_templates/'])
res['request'] = request
try:
t = lookup.get_template(template)
return HttpResponse(t.render(**res))
except:
if settings.DEBUG:
# display Mako's debug page on template error
return HttpResponse(exceptions.html_error_template()\
.render(), status=500)
raise
else:
# if not a dictionary or empty value, return literal result
return res
MAKOIFY.__name__ = func.__name__
MAKOIFY.__doc__ = func.__doc__
MAKOIFY.__dict__ = func.__dict__
return MAKOIFY
"""Example usage:
from makodecorator import mako
@mako("index.mako")
def foo(request, id):
return { "name" : "Robert Thomson" }
"""
Comments
What are the benefits of using Mako over the Django template system?
#
It's largely a matter of taste.. I liked perl's Mason, I liked Myghty, and I like Mako. Django templates seemed a little restrictive in some cases, although they're definitely fit-for-purpose. I'd rather put medium-complexity layout related logic in the template itself, and Mako facilitates that.
For more information, see Mako's website
#
I quite like Jinja nowadays. :-)
#