Skip to content

Commit

Permalink
add more stuffs to mako mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelnicolay committed Oct 17, 2012
1 parent b764fc0 commit 804d458
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions torneira/template/mako_engine.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
# coding: utf-8
from mako.exceptions import TopLevelLookupException
import logging
from mako.exceptions import TopLevelLookupException, html_error_template
from mako.lookup import TemplateLookup
from tornado.web import HTTPError

from torneira import settings


class MakoMixin(object):

def render_to_template(self, template_name, **context):
assert hasattr(settings, 'TEMPLATE_DIRS'), "Missing TEMPLATE_DIRS config"

lookup = TemplateLookup(directories=settings.TEMPLATE_DIRS, input_encoding='utf-8', output_encoding='utf-8', default_filters=['decode.utf8'])
lookup = TemplateLookup(directories=settings.TEMPLATE_DIRS,
input_encoding='utf-8', output_encoding='utf-8',
default_filters=['decode.utf8'])

try:
template = lookup.get_template(template_name)
except TopLevelLookupException:
raise HTTPError(500, "Template %s not found" % template_name)

return template.render(**context)
context.update({
'settings': settings,
'url_for': self.reverse_url
})

try:
return template.render(**context)
except Exception, e:

This comment has been minimized.

Copy link
@rcmachado

rcmachado Oct 18, 2012

Collaborator

If we catch all Exceptions here, the tornado write_error method on RequestHandler won't be called. Maybe we should overwrite write_error on MakoMixin to handle this error? If we do that, the user will be able to specify custom error handlers to catch exceptions.

This comment has been minimized.

Copy link
@marcelnicolay

marcelnicolay Oct 18, 2012

Author Owner

That is a good idea, but the problem is that the mako html_error_template render the stack trace in html. If we move this code to the write_error function we'll lose the stack trace.

In this point, the exception will be raised only if the template's render have a problem. We check if DEBUG is True, to show a pretty error message for developers.

For any other exception that occur, the write_error will be called.

if settings.DEBUG:
return html_error_template().render()
else:
logging.exception("Template %s could not be rendered" % template_name)
raise e

0 comments on commit 804d458

Please sign in to comment.