Coffin

Jinja2 adapter for Django
Download

Coffin Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Other/Proprietary Li...
  • Price:
  • FREE
  • Publisher Name:
  • David Cramer
  • Publisher web site:
  • http://code.google.com/u/dcramer/

Coffin Tags


Coffin Description

Jinja2 adapter for Django Coffin is a Jinja2 adapter for Django.Supported Django template functionalityCoffin currently makes the following Django tags available in Jinja: * {% cache %} - has currently an incompatibility: The second argument (the fragment name) needs to be specified with surrounding quotes if it is supposed to be a literal string, according to Jinja2 syntax. It will otherwise be considered an identifer and resolved as a variable. * {% load %} - is actually a no-op in Coffin, since templatetag libraries are always loaded. See also "Custom Filters and extensions". * {% spaceless %} * {% url %} - additionally, a "view"|url() filter is also available. * {% with %}Django filters that are ported in Coffin: * date * floatformat * pluralize (expects an optional second parameter rather than the comma syntax) * time * timesince * timeuntil * truncatewords * truncatewords_htmlThe template-related functionality of the following contrib modules has been ported in Coffin: * coffin.contrib.markup * coffin.contrib.syndication.RenderingSimply use the render_to_response replacement provided by coffin:from coffin.shortcuts import render_to_responserender_to_response('template.html', {'var': 1})This will render template.html using Jinja2, and returns a HttpResponse.404 and 500 handlersTo have your HTTP 404 and 500 template rendered using Jinja, replace the line:from django.conf.urls.defaults import *in your urls.py (it should be there by default), with:from coffin.conf.urls.defaults import *Custom filters and extensionsCoffin uses the same templatetag library approach as Django, meaning your app has a templatetags directory, and each of it's modules represents a "template library", providing new filters and tags.A custom Library class in coffin.template.Library can be used to register Jinja-specific components.Coffin can automatically make your existing Django filters usable in Jinja, but not your custom tags - you need to rewrite those as Jinja extensions manually.Example for a Jinja-enabled template library:from coffin import templateregister = template.Library()register.filter('plenk', plenk) # Filter for both Django and Jinjaregister.tag('foo', do_foo) # Django version of the tagregister.tag(FooExtension) # Jinja version of the tagregister.object(my_function_name) # A global function/objectOther things of noteWhen porting Django functionality, Coffin currently tries to avoid Django's silent-errors approach, instead opting to be explicit. Django was discussing the same thing before it's 1.0 release (*), but was constrained by backwards-compatibility concerns. However, if you are converting your templates anyway, it might be a good opportunity for this change.(*) http://groups.google.com/group/django-developers/browse_thread/thread/f323338045ac2e5eJinja2's TemplateSyntaxError (and potentially other exception types) are not compatible with Django's own template exceptions with respect to the TEMPLATE_DEBUG facility. If TEMPLATE_DEBUG is enabled and Jinja2 raises an exception, Django's error 500 page will sometimes not be able to handle it and crash. The solution is to disable the TEMPLATE_DEBUG setting in Django. See http://code.djangoproject.com/ticket/10216 for further information.coffin.template.loader is a port of django.template.loader and comes with a Jinja2-enabled version of get_template().coffin.template.Template is a Jinja2-Template that supports the Django render interface (being passed an instance of Context), and uses Coffin's global Jinja2 environment.coffin.interop exposes functionality to manually convert Django filters to Jinja2 and vice-versa. This is also what Coffin's Library object uses.A Jinja2-enabled version of add_to_builtins can be found in the django.template namespace.Things not supported by CoffinThese is an incomplete list things that Coffin does not yet and possibly never will, requiring manual changes on your part: * The slice filter works differently in Jinja2 and Django. Replace it with Jinja's slice syntax: x. * Jinja2's default filter by itself only tests the variable for existance. To match Django's behaviour, you need to pass True as the second argument, so that it will also provide the default value for things that are defined but evalute to False * Jinja2's loop variable is called loop, but Django's forloop. * Implementing an equivalent to Django's cycle-tag might be difficult, see also Django tickets #5908 and #7501. Jinja's own facilities are the forloop.cycle() function and the global function cycler. * The add filter might not be worth being implemented. {{ x+y }} is a pretty basic feature of Jinja2, and could almost be lumped together with the other Django->Jinja2 syntax changes. * Django-type safe strings passed through the context are not converted and therefore not recognized by Jinja2. For example, a notable place were this would occur is the HTML generation of Django Forms. * The {% autoescape %} tag is immensily difficult to port and currently not supported. * Literal strings from within a template are not automatically considered "safe" by Jinja2, different from Django. According to Armin Ronacher, this is a design limitation that will not be changed, due to many Python builtin functions and methods, whichyou are free to use in Jinja2, expecting raw, untainted strings and thus not being able to work with Jinja2's Markup string. Requirements: · Python · Django


Coffin Related Software