Permalink
Browse files

Merge pull request #6 from davedash/master

Using Django's Template Loader mechanism
  • Loading branch information...
2 parents 2506905 + a42f731 commit 7774456b867b3633c90cfef58aa42fbacb732409 @davedash davedash committed Jul 6, 2011
Showing with 30 additions and 14 deletions.
  1. +9 −14 docs/index.rst
  2. +21 −0 jingo/__init__.py
View
@@ -1,8 +1,9 @@
.. _jingo:
.. module:: jingo
-A page about Jingo.
-===================
+Jingo
+=====
+
Jingo is an adapter for using
`Jinja2 <http://jinja.pocoo.org/2/documentation/>`_ templates within Django.
@@ -26,20 +27,14 @@ or ::
def JINJA_CONFIG():
return {'the_answer': 41 + 1}
+You'll want to use jingo's template loader::
-Rendering
----------
-
-At this point, Jingo only provides two shortcuts for rendering templates.
-
-.. autofunction:: jingo.render
-
- The basic usage is to pass an ``HttpRequest`` and a template name. All the
- processors in ``settings.CONTEXT_PROCESSORS`` will be applied to the
- context, just like when you use a ``RequestContext`` in Django. Any extra
- keyword arguments are passed directly to ``http.HttpResponse``.
+ TEMPLATE_LOADERS = (
+ 'jingo.Loader',
+ )
-.. autofunction:: jingo.views.direct_to_template
+This will let you use ``django.shortcuts.render`` or
+``django.shortcuts.render_to_response``.
Template Helpers
View
@@ -6,6 +6,7 @@
from django import http
from django.conf import settings
from django.template.context import get_standard_processors
+from django.template.loader import BaseLoader
from django.utils.importlib import import_module
from django.utils.translation import trans_real
@@ -151,3 +152,23 @@ def wrapper(*args, **kw):
env = get_env()
register = Register(env)
+
+
+class Template(object):
+ def __init__(self, template):
+ self.template = template
+
+ def render(self, context):
+ # 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)
+
+
+class Loader(BaseLoader):
+ is_usable = True
+
+ def load_template(self, template_name, template_dirs=None):
+ template = env.get_template(template_name)
+ return Template(template), template.filename

0 comments on commit 7774456

Please sign in to comment.