Created
December 15, 2010 10:09
-
-
Save laiso/741832 to your computer and use it in GitHub Desktop.
Tipfyext-pyTenjin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
tipfyext.tenjin | |
~~~~~~~~~~~~~~~~ | |
pyTenjin template support for Tipfy. | |
Learn more about pyTenjin at http://www.kuwata-lab.com/tenjin/ | |
:copyright: 2010 by tipfy.org. | |
:license: BSD, see LICENSE.txt for more details. | |
""" | |
from __future__ import absolute_import | |
import tenjin | |
import tenjin.gae; tenjin.gae.init() | |
from tenjin.helpers import * | |
from werkzeug import cached_property | |
#: Default configuration values for this module. Keys are: | |
#: | |
#: templates_dir | |
#: Directory for templates. Default is `templates`. | |
default_config = { | |
'templates_dir': 'templates', | |
'postfix': '.pyhtml', | |
'layout': None, | |
} | |
class Tenjin(object): | |
def __init__(self, app, _globals=None, filters=None): | |
self.app = app | |
config = app.config[__name__] | |
dirs = config.get('templates_dir') | |
if isinstance(dirs, basestring): | |
dirs = [dirs] | |
postfix = config.get('postfix') | |
layout = config.get('layout') | |
self.engine = tenjin.Engine(path=dirs, postfix=postfix, layout=layout) | |
""" | |
self.environment = TemplateLookup(directories=dirs, | |
output_encoding='utf-8', encoding_errors='replace') | |
""" | |
def render(self, _filename, context): | |
"""Renders a template and returns a response object. | |
:param _filename: | |
The template filename, related to the templates directory. | |
:param context: | |
Keyword arguments used as variables in the rendered template. | |
These will override values set in the request context. | |
:returns: | |
A rendered template. | |
""" | |
return self.engine.render(_filename, context) | |
def render_template(self, _handler, _filename, context): | |
"""Renders a template and returns a response object. | |
:param _filename: | |
The template filename, related to the templates directory. | |
:param context: | |
Keyword arguments used as variables in the rendered template. | |
These will override values set in the request context. | |
:returns: | |
A rendered template. | |
""" | |
ctx = _handler.context.copy() | |
ctx.update(context) | |
return self.render(_filename, ctx) | |
def render_response(self, _handler, _filename, context): | |
"""Returns a response object with a rendered template. | |
:param _filename: | |
The template filename, related to the templates directory. | |
:param context: | |
Keyword arguments used as variables in the rendered template. | |
These will override values set in the request context. | |
""" | |
res = self.render_template(_handler, _filename, context) | |
return self.app.response_class(res) | |
@classmethod | |
def factory(cls, _app, _name, **kwargs): | |
if _name not in _app.registry: | |
_app.registry[_name] = cls(_app, **kwargs) | |
return _app.registry[_name] | |
class TenjinMixin(object): | |
"""Mixin that adds ``render_template`` and ``render_response`` methods | |
to a :class:`tipfy.RequestHandler`. It will use the request context to | |
render templates. | |
""" | |
# The Tenjin creator. | |
tenjin_class = Tenjin | |
@cached_property | |
def tenjin(self): | |
return self.tenjin_class.factory(self.app, 'tenjin') | |
def render_template(self, _filename, context): | |
return self.tenjin.render_template(self, _filename, context) | |
def render_response(self, _filename, context): | |
return self.tenjin.render_response(self, _filename, context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment