Jussi Arpalahti
unread,May 2, 2012, 4:36:59 AM5/2/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to aspen-users
Hi.
I wrote a basic Jinja2 simplate renderer (see below). I'd like to clarify some things before I submit a pull request.
- how to decide template encoding? is it configuration's dynamic encoding or what?
- how to implement error handling and pretty error messages?
-> with my current implementation tracebacks go to server console
- change reloading: how to get aspen's information about template updates to jinja2 or how to reload them myself?
-> Jinja2 has excellent support for reloading, but what information is available in Aspen renderer when changes happen
- where to put my renderer in aspen: just as a manually configurable module or put the code inside aspen like tornado and pymustache is?
-> so that I could make a better pull request when the code is finished.
Below is the code. Some values are hard coded to ease testing.
from aspen.rendering import Renderer, Factory
from jinja2 import BaseLoader, TemplateNotFound, Environment, FileSystemLoader, Template
def no():
return False # Jinja2 asks the given function if template should be reloaded, default is always
class SimplateLoader(BaseLoader):
def get_source(self, environment, template):
return (unicode(open("root/%s" % template).read(), 'utf-8'), template, no)
def load(self, environment, name, globals=None):
if not globals.has_key("raw"):
return super(SimplateLoader, self).load(environment, name, globals)
else:
code = environment.compile(globals["raw"].decode("utf-8"), name, name)
return environment.template_class.from_code(environment, code,
globals, no)
class Jinja2Renderer(Renderer):
def compile(self, filepath, raw):
return self.meta.get_template(filepath, globals={"raw":raw})
def render_content(self, compiled, context):
return compiled.render(context).encode("utf-8")
class Jinja2Factory(Factory):
Renderer = Jinja2Renderer
def __init__(self, configuration):
self.env = Environment(loader = SimplateLoader())
super(Jinja2Factory, self).__init__(configuration)
def compile_meta(self, configuration):
return self.env
Also, I run into some oddities. When trying to print context argument from Jinja2Renderer.render_content for debugging, it failed with this:
Traceback (most recent call last):
File "/local/lib/python2.7/site-packages/aspen/website.py", line 69, in handle
response = request.resource.respond(request)
File "/local/lib/python2.7/site-packages/aspen/resources/dynamic_resource.py", line 60, in respond
response = self.get_response(context)
File "/local/lib/python2.7/site-packages/aspen/resources/negotiated_resource.py", line 153, in get_response
response.body = render(context)
File "/local/lib/python2.7/site-packages/aspen/rendering.py", line 122, in __call__
return self.render_content(self.compiled, context)
File "/home/arpalah/projekti/yber/root/jinja2_renderer.py", line 35, in render_content
print context
File "/local/lib/python2.7/site-packages/aspen/http/request.py", line 236, in __repr__
return str.__repr__(str(self))
File "/local/lib/python2.7/site-packages/aspen/http/request.py", line 232, in __str__
self._raw = fmt % (self.line.raw, self.headers.raw, self.body.raw)
File "/local/lib/python2.7/site-packages/aspen/http/request.py", line 568, in raw
self._raw = "".join(self.s_iter)
TypeError
When I mistakenly returned an unicode object directly from Jinja2's template render and not the bytestring, my UTF-8 content was somehow converted to ISO-8859-1. With --charset_dynamic=UTF-8 --charset_static=UTF-8 the behaviour stayed the same. This relates to my encoding question, that is how to decide on which enconding to use for reading templates and writing responses.
Other than that, developing Jinja2 renderer was rather smooth.
Jussi