Templating - Python Wiki
Python Wiki Python Wiki / Python Wiki / Archive / Templating View as Markdown Templating ¶ Legacy Wiki Page This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org . 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. string.Template (python 3.x) in the python 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 ctemplate-python - fast templating with simple if/loop logic based on libCtemplate 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 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"> <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 obje
The rest of this page is on the live site.