Python XML Templating Language


Python is a great language for developing web applications, but it lacks a clean strategy for outputting dynamic pages. Interpolating a Python code hierarchy with the output markup hierarchy tends to produce difficult-to-read code, and Python’s reliance on indenting makes it impossible to format the code to fit within the markup hierarchy.

The work done by the JSR-052 group on templating in Java demonstrates the benefits of a taglib-style approach, using a single code and markup hierarchy to express the design more clearly. It is the aim of PXTL to bring something similar to Python, whilst improving on the design of JSTL where it is inflexible or confusing (often due to the design of JSP/taglibs or Java in general) and working around the difficulties inherent in using a whitespace-significant language for templating.

Unlike JSTL (and the many other templating systems available for Python), PXTL is a pure XML-based templating system. Source files must be well-formed XML; output is in XML, unless the optional legacy-HTML or plain-text recoding feature is enabled.

Nesting errors in code using JSTL taglibs can be hard to track down, as the source document is not well-formed and cannot be validated. (Additionally, the errors that result from ill-formed documents are very unhelpful.) It is hoped that sticking to well-formed XML will ease debugging.

(In many cases, it is even theoretically possible to prove programmatically that a PXTL template will always produce output that will validate to a particular schema, without actually having to run the code in the template. Such a tool has not been written yet, however.)

Where JSTL/JSP is forced to use a separate expression language (EL) to avoid Java’s inconvenience for writing expressions, PXTL can use normal Python. The EL habit of suppressing exceptions is eschewed as it hinders debugging. PXTL’s scoping rules are also much simpler than those of JSP, so as to aid rapid development.

PXTL files are intended only for use as an output template. They are not a web application framework. Using them encourages clean separation of action and view, but does not require it; architecture is left to the author.