Python Template Systems
Background Information
Most of these Web Template
Systems can be
applied to any text-based document, not just web pages, but that is
the main purpose I consider them for here.
A Web template system describes the software and methodologies used to
produce web pages and for deployment on websites and delivery over the
Internet. Such systems process web templates, using a template
engine. It is a web publishing tool present in content management
systems, software frameworks, HTML editors, and many other contexts.
-- Wikipedia
In case it's not clear to you programmers, a template system is to
authoring web pages what include files, macros, and inheritance are to
writing programs. It has the potential to take a lot of drudgery out
of writing web pages, since having a consistent "look and feel" to
your pages requires each page to share some stuff with other pages.
These are all written in python, for python programmers. I do not
know if they can be processed by tools written in other languages,
though it is theoretically possible.
Related Pages
Templates: The Right Amount of Push
The Template Systems
- is not just embedded python
- consists of variables (substituted values) and tags (logic constructs)
- "if", "for" tags for conditionals and looping
- can generate any text output format, not just XML/HTML
- variables are actually python objects, so have methods, dictionaries, etc.
- supports Unix-like "filters" (pipe char) for transforming variables
- supports template inheritance (saves coding, just like in OOP)
- automatic escaping makes XSS vulns easier to avoid
- also magically avoids double-escaping (escaped vars are different type than unescaped)
- does cheetah-like name mapping and autocalling (see later)
- templates can have any file extension
- consists of variables and expressions
- variables can be modified by Unix-like filters (pipe char)
- variables also can be modified by "tests" (e.g. is divisibleby(3))
- a little more control over whitespace than django
- two kinds of escaping to prevent interpretation by Jinja2<
- two ways to mark a statement, one with nested tags, another with line prefixes
- supports template inheritance
- (inherited) blocks (of template) can be nested
- supports automatic escaping but not on by default
- safety information can get lost if you use python string methods, leading to double-escaping
- supports macros, which are parameterized chunks of text, akin to functions in programming
- allows you to pass macros to another macro (higher-order functions)
- can include templates
- expressions are similar to python expressions
- supports i18n
- does cheetah-like name mapping and autocalling
- sandboxed, designer-friendly and quite fast, text template engine
- can generate any text-based format
- integrates tightly with WebWare for python
- template definitions conventionally end in .tmpl
- this compiler actually takes a template and generates python code!
- templates have placeholders which consist of variable names and other identifiers
- namespace lookup of variables is flexible
- placeholders can be top-level, expressions, and lvalues
- inheritance maps quite clearly onto python inheritance
- uses a "namemapper" to make dictionaries and object instances interchangable (dot versus brace unimportant)
- does "autocalling" so that you don't need parens to call a method
- has a nice comparison with other template engines (though none I review here)
- borrows from Django templates, Cheetah, Myghty, and Genshi
- embedded python
- used by python.org and reddit.com
- based on and grew out of [Myghty python web template framework(http://www.myghty.org/)
- default template for Pylons web framework (notably, Pylons grew out of Myghty; small world eh?)
- is as fast as any of the other popular approaches (cheetah, django, myghty, genshi, kid)
- inheritance is multi-zoned - similar to "blocks" in other template systems
- can traverse inheritance chain towards parent or child
- inheritance is dynamic! can define a function instead of a filename to specify inherits-from relationship
- simple way to cache rendered templates (unbounded or fixed-size)
- can render and encode Unicode
- plugins for Turbogears, Pylons, integration with WSGI, syntax highlighting with pygments, i18n via Babel
- supports expressions, filters, flow control structures, tags for commands to Mako
- can embed arbitrary python code blocks
- def is essentially a callable function
- can use "next" to refer to the child template, "parent" for the supertemplate
- namespaces can be manipulated and inherit attributes from others
- can define "default filters" for expressions on a page
- supports i18n via unicode
- relatively simplistic caching
- not XML-based
- very fast, a mix of ERB, Mason, and Django templates
Genshi is a Python library that provides an integrated set of
components for parsing, generating, and processing HTML, XML or other
textual content for output generation on the web.
The main feature is a template language that is smart about markup:
unlike conventional template languages that only deal with bytes and
(if you're lucky) characters, Genshi knows the difference between
tags, attributes, and actual text nodes, and uses that knowledge to
your advantage. [...]
- essentially embedded python
- can access dictionaries using "dotted notation", like cheetah's namemapper
- can use py:def to reuse snippets of code
- can use XPaths to select other elements and extract stuff from them
- templates are XML-based, which tends to be slower
- possibly the best XML template engine
- cross between XSLT, TAL, PHP
- numerous bugs on site prevented me from viewing it<
- generates XML only! (e.g. XHTML, RSS, Atom, FOAF, RDF, XBEL, XSLT, RelaxNG, Schematron, SOAP)
- guarantees well-formed XML given a valid template
TODO: finish this