From: Armin Ronacher Date: Tue, 29 May 2007 21:07:48 +0000 (+0200) Subject: [svn] added new jinja unittest and added snipped contributed by Bryan McLemore. X-Git-Tag: 2.0rc1~312 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5b4e9754c1db65a279886b36b0d0d7402d15c186;p=jinja2.git [svn] added new jinja unittest and added snipped contributed by Bryan McLemore. --HG-- branch : trunk --- diff --git a/docs/src/devrecipies.txt b/docs/src/devrecipies.txt index b5dc098..bad76c0 100644 --- a/docs/src/devrecipies.txt +++ b/docs/src/devrecipies.txt @@ -73,3 +73,31 @@ And use it: Also keep in mind that Jinja knows about keywords, thus you cannot have a filter that is called `pluralize` for example. + + +Using Jinja in Django +===================== + +This snippet was contributed by Bryan McLemore. It provides a `render_to_response` +function similar to the one shipped with django just that it uses Jinja for +rendering. It applies the context processors on the context and consumes a +`RequestContext`: + +.. sourcecode:: jinja + + from django.template.context import get_standard_processors + from django.http import HttpResponse + from jinja import Environment, FileSystemLoader, ChoiceLoader + from django.conf import settings + + loaders = [] + for location in settings.TEMPLATE_DIRS: + loaders.append(FileSystemLoader(location)) + env = Environment(loader=ChoiceLoader(loaders)) + + def render_to_response(template, context, request=None): + template = env.get_template(template) + if request: + for processor in get_standard_processors(): + context.update(processor(request)) + return HttpResponse(template.render(context)) diff --git a/docs/src/frameworks.txt b/docs/src/frameworks.txt index 952dd00..930b001 100644 --- a/docs/src/frameworks.txt +++ b/docs/src/frameworks.txt @@ -115,6 +115,16 @@ Because nobody implemented this specification so far it's not documented here but in the sourcecode of the `plugin module`_. The specification itself is explained on the pocoo trac on the `General Template Interface`_ wiki page. + +Django +====== + +Using Jinja in django is straightforward because django has a pretty low +level response interface. Just have a look at the `developer recipies`_, +there are some examples for django. + + .. _Pylons: http://www.pylonshq.com/ .. _General Template Interface: http://trac.pocoo.org/wiki/GeneralTemplateInterface .. _plugin module: http://trac.pocoo.org/browser/jinja/trunk/jinja/plugin.py +.. _developer recipies: devrecipies.txt diff --git a/tests/test_various.py b/tests/test_various.py index 62b516c..a4c148a 100644 --- a/tests/test_various.py +++ b/tests/test_various.py @@ -47,6 +47,13 @@ def test_raw(env): assert tmpl.render() == '{{ FOO }} and {% BAR %}' +def test_crazy_raw(): + from jinja import Environment + env = Environment('{', '}', '{', '}') + tmpl = env.from_string('{raw}{broken foo}{endraw}') + assert tmpl.render() == '{broken foo}' + + def test_cache_dict(): from jinja.utils import CacheDict d = CacheDict(3)