Differences between revisions 101 and 102
Revision 101 as of 2014-03-24 19:24:14
Size: 14978
Editor: DanyilBohdan
Comment: Fixed main link for HTMLgen.
Revision 102 as of 2014-03-24 19:29:49
Size: 14953
Editor: DanyilBohdan
Comment: Fixed link for Stan internal DSL.
Deletions are marked like this. Additions are marked like this.
Line 120: Line 120:
 * [[http://www.kieranholland.com/code/documentation/nevow-stan/|Stan]]  * [[http://docs.g-vo.org/meetstan.html|Stan]]

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:

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

  • Quik - A fast and lightweight Python template engine

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

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

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

  • 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

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.

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

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


CategoryTemplate

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

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