Differences between revisions 44 and 46 (spanning 2 versions)
Revision 44 as of 2008-09-19 18:38:46
Size: 10921
Editor: host153-72-dynamic
Comment: added "Best Template Engine"
Revision 46 as of 2008-11-15 13:59:50
Size: 11070
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
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. 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.
Line 17: Line 17:
    * [http://docs.python.org/lib/node40.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
    * [[http://docs.python.org/lib/node40.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 40: Line 40:
    * [http://airspeed.pythonconsulting.com/ Airspeed] - Velocity Templates for Python
    * ["Castalian"]
    * ["Cheetah"]
    * [[http://airspeed.pythonconsulting.com/|Airspeed]] - Velocity Templates for Python
    * [[Castalian]]
    * [[Cheetah]]
Line 44: Line 44:
    * [http://www.djangoproject.com/documentation/templates/ Django template system]
    * [http://www.nthwave.net/elements/ Elements] [[Anchor(Elements)]]
    * [[http://www.djangoproject.com/documentation/templates/|Django template system]]
    * [[http://www.nthwave.net/elements/|Elements]] <<Anchor(Elements)>>
Line 47: Line 47:
    * [http://evoque.gizmojo.org/ Evoque] - managed eval-based full-featured lightweight restricted-execution text-based pure python templating engine. [http://evoque.gizmojo.org/benchmark/ Performance benchmarks] show it to be more or less as fast as Mako, and faster on simpler templates.
    * [http://www.aerojockey.com/software/hrl HRL] Powerful macro preprocessor for HTML; macros can embed arbitrary Python code
    * [http://genshi.edgewall.org 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.''
    * [http://jinja.pocoo.org/ Jinja] - sandboxed textbased templating engine.
    * [http://www.makotemplates.org/ Mako] - new templating engine based on ideas from Myghty. ''Perfomance tests show that it is the fastest of all text based templating engines in Python.''
    * [http://www.myghty.org/ Myghty] inspired by Perl's Mason, replaced by Mako and [http://www.python.org/pypi/MyghtyUtils MyghtyUtils].
    * [http://www.mems-exchange.org/software/qpy/ 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 [http://evoque.gizmojo.org/benchmark/ lot faster] than Mako.
    * ["Spyce"]
    * [http://www.kuwata-lab.com/tenjin/ Tenjin] is the fastest template engine implemented in pure Python. It is about x2 faster than Mako, x3 than Cheetah, x9 than Django, x60 than Kid.
    * [http://tt2.org/python/index.html Template Toolkit] - Python port of Perl template engine
    * [http://the-next-please.sourceforge.net/manual.html thrases] - format-free Python needing just ~~ for separating phrases (as the semicolon in Python or C). Template.__init__() analyses, which phrases are python and which not, building a python script for exec(). This script does only contain minimal overhead - Template.render() is near to the theoretical maximum speed. Beside creating a StringIO Template.render() can also write directly on a file descriptor for improved performance.
    * [http://robinparmar.com/wasp.html Wasp] - simplicity itself: two tags plus free-form Python
    * [[http://evoque.gizmojo.org/|Evoque]] - managed eval-based full-featured lightweight restricted-execution text-based pure python templating engine. [[http://evoque.gizmojo.org/benchmark/|Performance benchmarks]] show it to be more or less as fast as Mako, and faster on simpler templates.
    * [[http://www.aerojockey.com/software/hrl|HRL]] Powerful macro preprocessor for HTML; macros can embed arbitrary Python code
    * [[http://genshi.edgewall.org|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.''
    * [[http://jinja.pocoo.org/|Jinja]] - sandboxed textbased templating engine.
    * [[http://www.makotemplates.org/|Mako]] - new templating engine based on ideas from Myghty. ''Perfomance tests show that it is the fastest of all text based templating engines in Python.''
    * [[http://www.myghty.org/|Myghty]] inspired by Perl's Mason, replaced by Mako and [[http://www.python.org/pypi/MyghtyUtils|MyghtyUtils]].
    * [[http://www.mems-exchange.org/software/qpy/|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 [[http://evoque.gizmojo.org/benchmark/|lot faster]] than Mako.
    * [[Spyce]]
    * [[http://www.kuwata-lab.com/tenjin/|Tenjin]] is the fastest template engine implemented in pure Python. It is about x2 faster than Mako, x3 than Cheetah, x9 than Django, x60 than Kid.
    * [[http://tt2.org/python/index.html|Template Toolkit]] - Python port of Perl template engine
    * [[http://the-next-please.sourceforge.net/manual.html|thrases]] - format-free Python needing just ~~ for separating phrases (as the semicolon in Python or C). Template.__init__() analyses, which phrases are python and which not, building a python script for exec(). This script does only contain minimal overhead - Template.render() is near to the theoretical maximum speed. Beside creating a StringIO Template.render() can also write directly on a file descriptor for improved performance.
    * [[http://robinparmar.com/wasp.html|Wasp]] - simplicity itself: two tags plus free-form Python
Line 75: Line 75:
    * [#ClearSilver] - uses special elements/tags
    * [http://py-templates.sourceforge.net HTMLTemplate]
    * ["JonsPythonModules"] - uses special comment-like markers
    * [http://www.plope.com/software/meld3/ meld3] and [http://www.entrian.com/PyMeld PyMeld] are very similar
    * [http://code.google.com/p/pyxer/ Pyxer] - based on Genshi parser engine. Optimized for work with Google App Enginge (GAE)
    * ["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 attributes in XML/HTML templates and a specific character in text templates
    * [http://www.python.org/pypi/XSLTools XSLTools] - uses special attributes (with XML documents providing the data)
    * [http://gna.org/projects/pypa PyPa] - nested comment-delimited blocks that are accessible from Python code as objects.
    * [[#ClearSilver]] - uses special elements/tags
    * [[http://py-templates.sourceforge.net|HTMLTemplate]]
    * [[JonsPythonModules]] - uses special comment-like markers
    * [[http://www.plope.com/software/meld3/|meld3]] and [[http://www.entrian.com/PyMeld|PyMeld]] are very similar
    * [[http://code.google.com/p/pyxer/|Pyxer]] - based on Genshi parser engine. Optimized for work with Google App Enginge (GAE)
    * [[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 attributes in XML/HTML templates and a specific character in text templates
    * [[http://www.python.org/pypi/XSLTools|XSLTools]] - uses special attributes (with XML documents providing the data)
    * [[http://gna.org/projects/pypa|PyPa]] - nested comment-delimited blocks that are accessible from Python code as objects.
Line 89: Line 89:
    * [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
    * [http://cherrytemplate.python-hosting.com/ CherryTemplate]
    * [[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 HTML-like elements/tags and supports compilation
    * [[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
    * [[http://cherrytemplate.python-hosting.com/|CherryTemplate]]
Line 100: Line 100:
    * [http://www.methods.co.nz/asciidoc/ AsciiDoc]
    * [http://www.freewisdom.org/projects/python-markdown/ Markdown]
    * [[http://www.methods.co.nz/asciidoc/|AsciiDoc]]
    * [[http://www.freewisdom.org/projects/python-markdown/|Markdown]]
Line 104: Line 104:
    * [http://txt2tags.sourceforge.net/ txt2tags]
    * [http://devsuki.com/pottymouth/ PottyMouth] (for untrusted text input)
    * [[http://txt2tags.sourceforge.net/|txt2tags]]
    * [[http://devsuki.com/pottymouth/|PottyMouth]] (for untrusted text input)
Line 113: Line 113:
   * [http://breve.twisty-industries.com/ Breve]    * [[http://breve.twisty-industries.com/|Breve]]
Line 120: Line 120:
   * [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://pylonshq.com/WebHelpers/module-webhelpers.htmlgen.html webhelpers.htmlgen] Kind of like HTMLGen, only much simpler. Like stan, only not.
   * [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366000 HTMLTags ]
   * [http://dustman.net/andy/python/HyperText/ HyperText]
   * [http://markup.sourceforge.net/ markup ] A light-weight and flexible HTML/XML generator
   * [http://www.livinglogic.de/Python/xist/ XIST ]
   * [http://pyhtmloo.sourceforge.net/ pyhtmloo ] pyhtmloo is a library that allows python developers to use HTML code like any other python objects.
   * [http://pypi.python.org/pypi/PyOoHtml/ PyOoHtml] - Python Object Oriented HTML - 'The future is reusable'
   * [[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://pylonshq.com/WebHelpers/module-webhelpers.htmlgen.html|webhelpers.htmlgen]] Kind of like HTMLGen, only much simpler. Like stan, only not.
   * [[http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366000|HTMLTags ]]
   * [[http://dustman.net/andy/python/HyperText/|HyperText]]
   * [[http://markup.sourceforge.net/|markup ]] A light-weight and flexible HTML/XML generator
   * [[http://www.livinglogic.de/Python/xist/|XIST ]]
   * [[http://pyhtmloo.sourceforge.net/|pyhtmloo ]] pyhtmloo is a library that allows python developers to use HTML code like any other python objects.
   * [[http://pypi.python.org/pypi/PyOoHtml/|PyOoHtml]] - Python Object Oriented HTML - 'The future is reusable'
Line 134: Line 134:
   * [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
   * [[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
Line 143: Line 143:
   * [http://freemarker.org/index.html FreeMarker] (with Jython data binding)
   * [http://java.sun.com/products/jsp Java Server Pages, JSP]
   * [http://jakarta.apache.org/velocity/index.html Velocity]
   * [http://www.webmacro.org/ WebMacro]
   * [[http://freemarker.org/index.html|FreeMarker]] (with Jython data binding)
   * [[http://java.sun.com/products/jsp|Java Server Pages, JSP]]
   * [[http://jakarta.apache.org/velocity/index.html|Velocity]]
   * [[http://www.webmacro.org/|WebMacro]]
Line 150: Line 150:
   * [[Anchor(ClearSilver)]] ClearSilver (HTML generation)    * <<Anchor(ClearSilver)>> ClearSilver (HTML generation)
Line 154: Line 154:
   * [http://pyoohtml.appspot.com/best-template-engine Best Template Engine] show your votes about these engines.    * [[http://pyoohtml.appspot.com/best-template-engine|Best Template Engine]] show your votes about these engines.

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

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 lightweight restricted-execution text-based pure python templating engine. 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 - sandboxed textbased templating engine.

  • Mako - new templating engine based on ideas from Myghty. Perfomance tests show that it is the fastest of all text based templating engines in Python.

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

  • Spyce

  • Tenjin is the fastest template engine implemented in pure Python. It is about x2 faster than Mako, x3 than Cheetah, x9 than Django, x60 than Kid.

  • Template Toolkit - Python port of Perl template engine

  • thrases - format-free Python needing just ~~ for separating phrases (as the semicolon in Python or C). Template.init() analyses, which phrases are python and which not, building a python script for exec(). This script does only contain minimal overhead - Template.render() is near to the theoretical maximum speed. Beside creating a StringIO Template.render() can also write directly on a file descriptor for improved performance.

  • Wasp - simplicity itself: two tags plus free-form Python

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

  • 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

  • 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

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

Best Template Engine

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

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