5915
Comment: Attempted to categorize the templating engines.
|
7855
Added examples from older pages. Added XSLTools.
|
Deletions are marked like this. | Additions are marked like this. |
Line 13: | Line 13: |
=== 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. * [http://docs.python.org/lib/node109.html string.Template] in the standard library * [http://www.stringtemplate.org stringtemplate] - employs recursion in order to provide support for complicated templating whilst avoiding side-effects |
|
Line 15: | Line 22: |
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 templates visually rather different from the final output, as well as introducing problems for some XML-based tools. Nevertheless, 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. | 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. |
Line 28: | Line 51: |
* [http://www.stringtemplate.org stringtemplate] | |
Line 33: | Line 55: |
The following engines feature template documents whose sections are marked using special attributes (or, less frequently, special elements or tags). In some systems, the sections are then manipulated within program code: | 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. |
Line 42: | Line 75: |
* [http://psilib.sf.net/webstring.html webstring] uses ID attributes for markers * [http://www.python.org/pypi/XSLTools XSLTools] - uses special attributes (with XML documents providing the data) |
|
Line 45: | Line 80: |
* [http://genshi.edgewall.org/ Genshi] - Template engine inspired by Kid, supports both [http://genshi.edgewall.org/wiki/Documentation/xml-templates.html XML] and [http://genshi.edgewall.org/wiki/Documentation/text-templates.html plain-text] templates | |
Line 46: | Line 82: |
* [http://kid.lesscode.org/ Kid] - XML based, compiling template engine | * [http://www.kid-templating.org/ Kid] - XML based, compiling template engine |
Line 48: | Line 84: |
* [http://cherrytemplate.python-hosting.com/ CherryTemplate] | |
Line 63: | Line 100: |
* [http://genshi.edgewall.org/ Genshi] The genshi.builder module provides [http://genshi.edgewall.org/wiki/Documentation/builder.html simple markup generation] |
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; [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52305 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.
[http://docs.python.org/lib/node109.html string.Template] in the standard library
[http://www.stringtemplate.org stringtemplate] - employs recursion in order to provide support for complicated templating whilst avoiding side-effects
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.
[http://airspeed.pythonconsulting.com/ Airspeed] - Velocity Templates for Python
- ["Castalian"]
- ["Cheetah"]
[http://www.djangoproject.com/documentation/templates/ Django template system]
Anchor(Elements) [http://www.nthwave.net/elements/ Elements]
[http://www.aerojockey.com/software/hrl HRL] Powerful macro preprocessor for HTML; macros can embed arbitrary Python code
[http://wsgiarea.pocoo.org/jinja/ Jinja]
[http://www.myghty.org/ Myghty] inspired by Perl's Mason
- ["Spyce"]
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
[http://freespace.virgin.net/hamish.sanderson/htmltemplate.html HTMLTemplate]
["JonsPythonModules"] - uses special comment-like markers
[http://www.plope.com/software/meld3/ meld3] and [http://www.entrian.com/PyMeld PyMeld] are very similar
- ["pso"]
[http://pytan.com/public/sprite/ Sprite] - uses special comment-like markers
- ["teng"] - uses processing instruction-like markers
[http://psilib.sf.net/webstring.html webstring] uses ID attributes for markers
[http://www.python.org/pypi/XSLTools XSLTools] - uses special attributes (with XML documents providing the data)
In other systems, the annotations are actually evaluated in order to produce repeated sections to omit or to include sections, and so on:
[http://genshi.edgewall.org/ Genshi] - Template engine inspired by Kid, supports both [http://genshi.edgewall.org/wiki/Documentation/xml-templates.html XML] and [http://genshi.edgewall.org/wiki/Documentation/text-templates.html plain-text] templates
[http://htmltmpl.sourceforge.net/ htmltmpl] - uses special elements/tags
[http://www.kid-templating.org/ Kid] - XML based, compiling template engine
Anchor(SimpleTAL)[http://www.owlfish.com/software/simpleTAL/ SimpleTAL] - introduces a certain amount of logic but in an XML-compatible fashion
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.
[http://txt2tags.sourceforge.net/ txt2tags]
HTML Generation Packages
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.
[http://genshi.edgewall.org/ Genshi] The genshi.builder module provides [http://genshi.edgewall.org/wiki/Documentation/builder.html simple markup generation]
[http://starship.python.net/crew/friedrich/HTMLgen/html/main.html HTMLgen]
[http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366000 HTMLTags ]
[http://markup.sourceforge.net/ markup ] A light-weight and flexible HTML/XML generator
[http://pyhtmloo.sourceforge.net/ 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.
[http://www.ivy.fr/tahchee/ tahchee] - ["Cheetah"]-based static web site generator
[http://www.owlfish.com/software/PubTal/ PubTal] - [#SimpleTAL]-based static web site generator
- [#Elements]
[http://www.voidspace.org.uk/python/rest2web/] - Generates Websites from ReST contents
Java Templating Engines
The following templating engines are accessible or usable via Jython:
[http://freemarker.org/index.html FreeMarker] (with Jython data binding)
[http://java.sun.com/products/jsp Java Server Pages, JSP]
CPython-accessible C Templating Engines
Anchor(ClearSilver) ClearSilver (HTML generation)