Permalink
Browse files

Refactor Template.render()

- Template now subclasses Jinja2.Template, like it should have all along
- We now fire off template rendered signals.
- This now works in Django Debug Toolbar (thanks for the pointers @robhudson)
  • Loading branch information...
1 parent 844acb5 commit 5d73f69bb330d1f42504a3a5eb8dc2845ec51cc5 @davedash davedash committed Oct 24, 2011
Showing with 33 additions and 13 deletions.
  1. +30 −12 jingo/__init__.py
  2. +3 −1 jingo/tests/test_helpers.py
View
@@ -5,7 +5,7 @@
from django import http
from django.conf import settings
-from django.template.base import TemplateDoesNotExist
+from django.template.base import Origin, TemplateDoesNotExist
from django.template.context import get_standard_processors
from django.template.loader import BaseLoader
from django.utils.importlib import import_module
@@ -89,7 +89,7 @@ def get_context():
if not isinstance(template, jinja2.environment.Template):
template = env.get_template(template)
- return template.render(**get_context())
+ return template.render(get_context())
def load_helpers():
@@ -158,26 +158,44 @@ def wrapper(*args, **kw):
register = Register(env)
-class Template(object):
- def __init__(self, template):
- self.template = template
+class Template(jinja2.Template):
- def render(self, context):
+ def render(self, context={}):
+ """Render's a template, context can be a Django Context or a
+ dictionary.
+ """
# flatten the Django Context into a single dictionary.
context_dict = {}
- for d in context.dicts:
- context_dict.update(d)
- return self.template.render(context_dict)
+ if hasattr(context, 'dicts'):
+ for d in context.dicts:
+ context_dict.update(d)
+ else:
+ context_dict = context
+
+ class FakeRequestContext:
+ dicts = [context]
+ context = FakeRequestContext()
+
+ if settings.TEMPLATE_DEBUG:
+ from django.test import signals
+ self.origin = Origin(self.filename)
+ signals.template_rendered.send(sender=self, template=self,
+ context=context)
+
+ return super(Template, self).render(context_dict)
class Loader(BaseLoader):
is_usable = True
+ env.template_class = Template
def load_template(self, template_name, template_dirs=None):
if hasattr(template_name, 'rsplit'):
app = template_name.rsplit('/')[0]
if app in getattr(settings, 'JINGO_EXCLUDE_APPS', []):
raise TemplateDoesNotExist(template_name)
-
- template = env.get_template(template_name)
- return Template(template), template.filename
+ try:
+ template = env.get_template(template_name)
+ return template, template.filename
+ except jinja2.TemplateNotFound:
+ raise TemplateDoesNotExist(template_name)
@@ -11,9 +11,10 @@
from jingo import helpers
from jingo import register
+
def render(s, context={}):
t = jingo.env.from_string(s)
- return t.render(**context)
+ return t.render(context)
def test_f():
@@ -133,6 +134,7 @@ def test_url():
def url(x, *y, **z):
return '/' + x + '!'
+
@patch('django.conf.settings')
def test_custom_url(s):
# register our url method

0 comments on commit 5d73f69

Please sign in to comment.