Jinja 1.2

Jinja is a sandboxed template engine written in pure Python and licensed under the BSD license.

  Add it to your Download Basket!

 Add it to your Watch List!


Rate it!
send us
an update
BSD License 
3.5/5 12
Pocoo Team
ROOT \ Internet \ HTTP (WWW)
Jinja is a sandboxed template engine written in pure Python and licensed under the BSD license. It provides a Django-like non-XML syntax and compiles templates into executable Python code. It's basically a combination of Django templates and Python code.


Application logic is for the controller but don't try to make the life for the template designer too hard by giving him too few functionality.


{% extends 'base.html' %}
{% block title %}Memberlist{% endblock %}
{% block content %}
< ul >
{% for user in users %}
< li >< a href="{{ user.url|e }}" >{{ user.username|e }}< /a >< /li >
{% endfor %}
< /ul >
{% endblock %}

Here are some key features of "Jinja":

· Simple API. For basic usage just one function is needed:

from jinja import from_string
print from_string('Hello {{ data }}!').render(data='World')

· Sandboxed. The whole engine is completely sandboxed. A template designer won't be able to modify application data or execute dangerous code.
· Python expressions. You can use nearly every python expression. Not supported are the binary operators and list comprehensions / generator expressions.
· Inheritance. Jinja uses the same concept for inheritance Django uses. It's very powerful and easy to understand.
· Macros. Jinja provides so called macros that allow you to put often used template snippets into callable blocks:

{% macro dialog title, text %}
< div class="dialog" >
< h2 class="title" >{{ title }}< /h2 >
< div class="text" >{{ text }}< /div >
< /div >
{% endmacro %}

You can then use this block by calling it:

{{ dialog('Notification', 'Here the text for the macro.') }}

· Designer friendly. Jinja simplifies many things for a template designer. Loops can be used in a recursive way, filters are available to format values, loops know about their iteration etc.
· Dynamic Syntax. You don't like the Django block syntax? You can override the syntax elements on environment initialisation. It's no problem to use ASP/PHP/Ruby syntax, html comments for blocks etc.

What's New in This Release:

· environments now have a translator_factory parameter that allows to change the translator without subclassing the environment.
· fixed bug in buffet plugin regarding the package loader
· once again improved debugger.
· added groupby filter.
· added sameas test function.
· standalone parser. Jinja does not use the python parser any more and will continue having the same semantics in any future python versions. This was done in order to simplify updating Jinja to 2.6 and 3.0 and to support non python syntax.
· added support for expr1 if test else expr2 (conditional expressions)
· foo.0 as alias for foo[0] is possible now. This is mainly for django compatibility.
· the filter operators has a much higher priority now which makes it possible to do foo|filter + bar|filter.
· new AST. the return value of Environment.parse is now a Jinja AST and not a Jinja-Python AST. This is also the only backwards incompatible change but should not affect many users because this feature is more or less undocumented and has few use cases.
· tuple syntax returns tuples now and not lists any more.
· the print directive and {{ variable }} syntax now accepts and implicit tuple like the for and cycle tags. ({{ 1, 2 }} is an implicit alias for {{ (1, 2) }}` like ``{% for a, b in seq %} is for {% for (a, b) in seq %}.
· tests called with one parameter don't need parentheses. This gives a more natural syntax for the sameas test and some others: {{ foo is sameas bar }} instead of {{ foo is sameas(bar) }}. If you however want to pass more than one argument you have to use parentheses because {{ foo is sometest bar, baz }} is handled as {{ (foo is sometest(bar), baz) }}, so as tuple expression.
· removed support for octal character definitions in strings such as '14', use 'x0c' now.
· added regular expression literal. @/expr/flags equals re.compile(r'(?flags)expr'). This is useful for the matching test and probably some others.
· added set literal. We do not use python3's {1, 2} syntax because this conflicts with the dict literal. To be compatible with the regex literal we use @(1, 2) instead.
· fixed bug in get_attribute that disallowed retreiving attributes of objects without a __class__ such as _sre.SRE_Pattern.
· addded django.contrib.jinja which provides advanced support for django. (thanks Bryan McLemore)
· debugger is now able to rewrite the whole traceback, not only the first frame. (requires the optional debugger c module which is compiled automatically on installation if possible)
· if the set that is postfixed with a bang (!) it acts like the python 3 "nonlocal" keyword. This means that you can now override variables defined in the outer scope from within a loop.
· foo ~ bar is now a simpler alternative to foo|string + bar|string
· PackageLoader can now work without pkg_resources too
· added getattribute and getitem filter.
· added support for the pretty library.
· changed the way the MemcachedLoaderMixin creates the class so that it's possible to hook your own client in.

Last updated on April 1st, 2008

#template engine #Django templates #compile templates #sandbox #template #engine #Django

Add your review!