Jinja: Python template engine

Jinja logo


{% extends "layout.html" %}
{% block body %}
  {% for user in users %}
    <li><a href="{{ user.url }}">{{ user.username }}</a></li>
  {% endfor %}
{% endblock %}

Features (from Jinja website)

  • Sandboxed execution mode. Every aspect of the template execution is monitored and explicitly whitelisted or blacklisted, whatever is preferred. This makes it possible to execute untrusted templates.
  • powerful automatic HTML escaping system for cross site scripting prevention.
  • Template inheritance makes it possible to use the same or a similar layout for all templates.
  • High performance with just in time compilation to Python bytecode. Jinja2 will translate your template sources on first load into Python bytecode for best runtime performance.
  • Optional ahead-of-time compilation
  • Easy to debug with a debug system that integrates template compile and runtime errors into the standard Python traceback system.
  • Configurable syntax. For instance you can reconfigure Jinja2 to better fit output formats such as LaTeX or JavaScript.
  • Template designer helpers. Jinja2 ships with a wide range of useful little helpers that help solving common tasks in templates such as breaking up sequences of items into multiple columns and more.

Mako: Template library written in Python

Mako logo


From the Mako website:

Mako is a template library written in Python. It provides a familiar, non-XML syntax which compiles into Python modules for maximum performance. Mako’s syntax and API borrows from the best ideas of many others, including Django and Jinja2 templates, Cheetah, Myghty, and Genshi. Conceptually, Mako is an embedded Python (i.e. Python Server Page) language, which refines the familiar ideas of componentized layout and inheritance to produce one of the most straightforward and flexible models available, while also maintaining close ties to Python calling and scoping semantics.


<%inherit file="base.html"/>
    rows = [[v for v in range(0,10)] for row in range(0,10)]
    % for row in rows:
    % endfor

<%def name="makerow(row)">
    % for name in row:
    % endfor

Nunjucks: JavaScript templating language

Nunjucks logo


Example Template

{% extends "base.html" %}

{% block header %}
<h1>{{ title }}</h1>
{% endblock %}

{% block content %}
  {% for name, item in items %}
  <li>{{ name }}: {{ item }}</li>
  {% endfor %}
{% endblock %}

Builtin Filters

{{ foo | title }}
{{ foo | join(",") }}
{{ foo | replace("foo", "bar") | capitalize }}

Keyword Arguments

{{ foo(1, 2, bar=3, baz=4) }}
{{ bar | transform(level=2) }}

Template Inheritance

{% extends "base.html" %}

{% block header %}
<h3>{{ subtitle }}</h3>
{% endblock %}

{% block content %}
<h1>{{ page.title }}</h1>
<p>{{ page.content }}</p>
{% endblock %}

Asynchronous Templates

{% asyncAll item in items %}
  <li>{{ item.id | lookup }}</li>
{% endall %}

Thymeleaf: Extensible Java-based XML / XHTML / HTML5 template engine

Thymeleaf Logo

A Java templating language. I like how the syntax modifies attributes on existing tags like this:

<input type="text" th:field="*{name}" />

From the Thymeleaf website:

Thymeleaf is a Java library. It is an XML / XHTML / HTML5 template engine
(extensible to other formats) that can work both in web and non-web
environments. It is better suited for serving XHTML/HTML5 at the view layer of
web applications, but it can process any XML file even in offline environments.

It provides an optional module for integration with Spring MVC, so that you
can use it as a complete substitute of JSP in your applications made with this
technology, even with HTML5.

The main goal of Thymeleaf is to provide an elegant and well-formed way of
creating templates. Its Standard and SpringStandard dialects allow you to
create powerful natural templates, that can be correctly displayed by browsers
and therefore work also as static prototypes. You can also extend Thymeleaf by
developing your own dialects.

Sample markup

      <th th:text="#{msgs.headers.name}">Name</th>
      <th th:text="#{msgs.headers.price}">Price</th>
    <tr th:each="prod : ${allProducts}">
      <td th:text="${prod.name}">Oranges</td>
      <td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td>

FreeMarker: Java Templating Engine

Freemarker image

From the website:

FreeMarker is a “template engine”; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates.
It’s a Java package, a class library for Java programmers. It’s not an application for end-users in itself, but something that programmers can
embed into their products.

FreeMarker is designed to be practical for the generation of HTML Web pages, particularly by servlet-based applications following the MVC (Model
View Controller) pattern. The idea behind using the MVC pattern for dynamic Web pages is that you separate the designers (HTML authors) from the
programmers. Everybody works on what they are good at. Designers can change the appearance of a page without programmers having to change or
recompile code, because the application logic (Java programs) and page design (FreeMarker templates) are separated. Templates do not become
polluted with complex program fragments. This separation is useful even for projects where the programmer and the HTML page author is the same
person, since it helps to keep the application clear and easily maintainable.

Although FreeMarker has some programming capabilities, it is not a full-blown programming language like PHP. Instead, Java programs prepare the
data to be displayed (like issue SQL queries), and FreeMarker just generates textual pages that display the prepared data using templates.

We use Apache Velocity extensively at work in our web content, but it hasn’t seen much in the way of updates for quite some time.