Differences between revisions 140 and 141
Revision 140 as of 2016-12-05 22:36:16
Size: 16101
Comment: added a link to the Spitfire templating system.
Revision 141 as of 2019-12-15 07:18:08
Size: 16004
Comment: Remove py2 reference
Deletions are marked like this. Additions are marked like this.
Line 14: Line 14:
 * [[http://docs.python.org/2/library/string.html#template-strings|string.Template (python 2.x)]] & [[https://docs.python.org/3.4/library/string.html#template-strings|string.Template (python 3.x)]] in the python standard library.  * [[https://docs.python.org/3.4/library/string.html#template-strings|string.Template (python 3.x)]] in the python standard library.

Templating in Python

Templating, and in particular web templating is a way to represent data in different forms. These forms often (but not always) intended to be readable, even attractive, to a human audience. Frequently, templating solutions involve a document (the template) and data. Template usually looks much like the final output, with placeholders instead of actual data (or example data in simplified form), bears common style and visual elements. Data which is presented using that template may be also separated in two parts - data required to be rendered, and data required for template itself (navigation elements if it is a site, button names if it is some UI). Combining template+data produces the final output which is usually (but not always) a web page of some kind.

Templating Engines

There are many, many different HTML/XML templating packages and modules for Python that provide different feature sets and syntaxes. These libraries usually assume that you know how to write HTML or XML.

The number of templating engines is so great because the mechanisms involved are pretty easy to write in Python, at least for a fairly basic template engine; this recipe from the Python Cookbook shows how easy it is.

Engines using Value Substitution

The simplest form of templating engine is that which merely substitutes values into a template in order to produce the final output. They sometimes provide tags for if statements or loops, but they are crude.

Engines Mixing Logic into Templates

A popular approach with templating engines is to embed logic or control-flow statements into the templates themselves in a way that can make the the final output appear rather different from the original template. For example:

  for item in items:
      <td><%= item.name %></td>

The introduction of such logic may also cause problems for some XML-based tools. Despite these shortcomings, such templating engines may be more applicable to non-Web templating problems or for situations where separating logic from content may actually make the solution harder to understand.

  • Airspeed - Velocity Templates for Python

  • Castalian

  • Chameleon - fast page template implementation which compiles markup templates into python byte code. Used by Pyramid, Zope, Plone and Grok projects.

  • Cheetah

  • CubicTemp

  • Django template system

  • Elements

  • EmPy

  • Evoque page on pypi - managed eval-based full-featured templating engine, for Python 2.4, 2.5, 2.6 and 3.0, features such as unicode, dynamic overlays, format-extensible automatic quoting, in-process sandbox, et cetera, while still remaining small, simple and extremely fast -- performance benchmarks show it to be more or less as fast as Mako, and faster on simpler templates.

  • HRL (HTML Redemption Language) - Powerful macro preprocessor for HTML; macros can embed arbitrary Python code. ( 2010-07-04, Officially discontinued)

  • Genshi - XML-based templating engine, used in the popular python tool trac. Performance tests show that it is the fastest of all xml based templating engines in Python.

  • Jinja 2 - an extensible, sandboxed text-based templating engine with Django-like syntax (but faster).

  • Ashes - A Python 2/3-compatible version of the Dust templating language, implemented in a single file, also usable through a built-in CLI. Enables template reuse on the frontend through Dust.js.

  • Mako - a fast, non-xml, templating engine based on ideas from Myghty.

  • moody-templates - A fast, extensible templating engine for Python 3 with Django-like syntax.

  • Myghty inspired by Perl's Mason, replaced by Mako and MyghtyUtils.

  • Qpy provides a convenient mechanism for generating safely-quoted html text from python code. It does this by implementing a quoted-string data type and a modification of the python compiler.

  • Quik - A fast and lightweight Python template engine

  • PML is a high performance template engine implemented in Python, it supports many advanced features such as template filters, output filters, and more.

  • pyratemp - a very small (<500 LOC) but complete template-engine, using restricted python-expressions. There are also some benchmarks and comparisons of different template-engines.

  • Spitfire - A super fast Cheetah-like template system used by YouTube.

  • Spyce

  • SUIT - powerful template engine that allows one to define their own syntax to transform templates by using rules.

  • Tempita - a fairly simple, small templating language with full Python expressions

  • Template Toolkit - Python port of Perl template engine

  • Templet - a 90-line BSD-licensed utility that defines @stringfunction and @unicodefunction python function decorators for simple, robust, and speedy templating.

  • Templite+ - A light-weight, fully functional, general purpose templating engine

  • Tenjin is a fast template engine implemented in pure Python. Some benchmarks have shown it to be about x2 faster than Mako, x3 than Cheetah, x9 than Django, x60 than Kid in some situations. However 50% slower wheezy.template.

  • Texthon - Python-eval based template engine with a focus on generating readable code.

  • thrases - format-free Python needing just needing a reserved string (default: ~~) for separating phrases. Template.init() analyses, which phrases are python and which not, building a python script for exec(). This script is containing only minimal overhead then - Template.render() is near to the theoretical maximum speed. Template.render() can also write directly on a file descriptor for improved performance.

  • Tonnikala - XML syntax that is very close to that of Kajiki. Tonnikala writes code as Abstract Syntax Trees and optimizes the resulting trees extensively

  • trender - A fast, simple and stand-alone Python template engine.

  • wheezy.template is written in pure Python code. It is a lightweight template library. The design goals achived:

    • Compact, Expressive, Clean: Minimizes the number of keystrokes required to build a template. Enables fast and well read coding.
    • Intuitive, No time to Learn: Basic Python programming skills plus HTML markup. You are productive just from start. Use full power of Python with minimal markup required to denote python statements.
    • Do Not Repeat Yourself: Master layout templates for inheritance; include and import directives for maximum reuse.
    • Blazingly Fast: Maximum rendering performance: ultimate speed and context preprocessor features.

Engines with Annotated Templates

The following engines feature template documents whose sections are marked using special attributes (or, less frequently, special elements or tags). For example:

<table annotation:element="items">
  <tr annotation:element="item">

In some systems, the sections are then manipulated within program code; in others, the template structure indicates sections which are to be repeated, omitted, and so on, and the templating system then merges the template with some data structure provided by the program. Generally, the reason for annotating templates in this way (particularly through the use of attributes) is to better support the editing of such templates in XML-based tools which might otherwise complain about or damage template information if it were not included carefully in documents.

  • ClearSilver - uses special elements/tags

  • HTMLTemplate - special attributes denote HTML elements that can be manipulated as Python objects (As of 2013-12-05, this project is no longer under active development.)

  • JonsPythonModules - uses special comment-like markers

  • meld3 and PyMeld are very similar

  • Pyxer - based on Genshi parser engine. Optimized for work with Google App Enginge (GAE)

  • pso

  • Sprite - uses special comment-like markers

  • teng - uses processing instruction-like markers

  • webstring - uses attributes in XML/HTML templates and a specific character in text templates

  • XSLTools - uses special attributes (with XML documents providing the data)

  • PyPa - nested comment-delimited blocks that are accessible from Python code as objects.

  • TDI - Manipulate tagged HTML/XML elements with normal Python code. Fast.

In other systems, the annotations are actually evaluated in order to produce repeated sections, to omit or include sections, and so on:

  • Genshi - Template engine inspired by Kid, supports both XML and plain-text templates

  • kajiki - Template engine inspired by Genshi

  • htmltmpl - uses HTML-like elements/tags and supports compilation

  • Kid - XML based, compiling template engine

  • SimpleTAL - introduces a certain amount of logic but in an XML-compatible fashion

  • CherryTemplate - is a Pythonic HTTP toolkit.

HTML Shorthand Processors

The libraries in this section implement simpler markup languages that can be automatically converted to HTML. This lets you avoid having to write HTML by hand.

Template engines implemented as Internal DSL's

These engines are implemented as an internal DSL, that is, they don't process text into markup, rather they represent the final document as actual Python code and data structures. See: An overview of the benefits of this internal DSL approach vs external template languages

HTML Generation Packages

Many of these links are dead. Perhaps someone more knowledgeable might want to fix or prune them.

These packages are not really templating systems in that they do not typically employ a template document as such to define the form of the output they produce, but they can be useful in applications where it is more convenient to programmatically generate output.

  • WebElements allows creating html documents using python objects that represent their DOM equivalents, inspired by QT.

  • Genshi The genshi.builder module provides simple markup generation

  • HTMLgen A old-school module first written for Python 1.x. Debian's package maintainers' patches bring it into the twenty-first century with Python 2.7 compatibility. Mirrored on GitHub.

  • webhelpers.htmlgen Kind of like HTMLGen, only much simpler. Like stan, only not.

  • html Provides a simple syntax to generate HTML, XHTML and XML.

  • HTMLTags

  • HyperText

  • markup A light-weight and flexible HTML/XML generator

  • XIST

  • pyhtmloo pyhtmloo is a library that allows python developers to use HTML code like any other python objects.

  • Yattag Provides a readable way to write HTML or XML within Python using indented blocks instead of <tag>...</tag> constructs.

Static Website Generators

Static website generators are more than templating engines in that they create the whole site structure, not just individual files. While templating is an important part of their function, determining the site structure and incorporating structural information in the output (for example to automatically generate navigational elements) is what really makes a static website generator a useful tool.

See StaticSiteGenerator for the list.

Java Templating Engines

The following templating engines are accessible or usable via Jython:

CPython-accessible C Templating Engines

  • ClearSilver - HTML generation, uses HDF as input format


Templating (last edited 2019-12-15 07:18:08 by FrancesHocutt)

Unable to edit the page? See the FrontPage for instructions.