Skip to content

Commit

Permalink
Merge pull request #6 from davedash/master
Browse files Browse the repository at this point in the history
Using Django's Template Loader mechanism
  • Loading branch information
davedash committed Jul 6, 2011
2 parents 2506905 + a42f731 commit 7774456
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
23 changes: 9 additions & 14 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions jingo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.