Jinja Templates
Jinja is a sandboxed template engine written in pure Python 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.
The latest Jinja Version is 1.2, you can get it from the download page.
Philosophy
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.
Nutshell
{% 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 %}
Features
- 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 bitwise 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:
You can then use this block by calling it:
{% macro dialog title, text %} <div class="dialog"> <h2 class="title">{{ title }}</h2> <div class="text">{{ text }}</div> </div> {% endmacro %}
{{ 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.