Differences between revisions 77 and 78
Revision 77 as of 2011-02-05 12:12:14
Size: 12905
Editor: host-88-217-136-187
Comment:
Revision 78 as of 2011-04-05 22:01:54
Size: 13097
Editor: techtonik
Comment: add mustache to value substitution templates
Deletions are marked like this. Additions are marked like this.
Line 19: Line 19:
    * [[http://mustache.github.com/|mustache]] - logic-less templates based on [[http://code.google.com/p/google-ctemplate/|CTemplate]] with implementation in many languages including Python

Templating in Python

Templating, and in particular Web templating, involves the presentation of information in a form which is often (but not always) intended to be readable, even attractive, to a human audience. Frequently, templating solutions involve a document (the template) which may look somewhat like the final output but perhaps in a simplified or stylized form, along with some data which must be presented using that template; combining these two things produces the final output which in Web templating 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.

  • string.Template in the standard library

  • stringtemplate - employs recursion in order to provide support for complicated templating whilst avoiding side-effects

  • mustache - logic-less templates based on CTemplate with implementation in many languages including Python

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:

<table>
  <%
  for item in items:
    %>
    <tr>
      <th>Name</th>
      <td><%= item.name %></td>
    </tr>
    <%
  %>
</table>

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

  • Cheetah

  • CubicTemp

  • Django template system

  • Elements

  • EmPy

  • Evoque - managed eval-based full-featured templating engine, for Python 2.4, 2.5, 2.6 and 3.0, with state of the art 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 Powerful macro preprocessor for HTML; macros can embed arbitrary Python code

  • 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).

  • 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. A lot faster than Mako.

  • 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.

  • 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

  • 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.

  • Template Toolkit - Python port of Perl template engine

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

  • 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.

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">
    <th>Name</th>
    <td>{name}</td>
  </tr>
</table>

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

  • 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.

In other systems, the annotations are actually evaluated in order to produce repeated sections to omit or to 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

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.

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.

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.

Java Templating Engines

The following templating engines are accessible or usable via Jython:

CPython-accessible C Templating Engines

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

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