The Python StringTemplate Template Engine

Brought to you by that maniac that brings you ANTLR! Terence Parr
University of San Francisco
parrt@cs.usfca.edu
Copyright 2003-2006
http://www.stringtemplate.org
(StringTemplate released under BSD License)

Python version created by

Marq Kole
marq.kole at xs4all.nl
Copyright 2003-2006

Download PyStringTemplate

Introduction

StringTemplate is a template engine for generating source code, web pages, emails, or any other formatted text output. It evolved over years of effort developing the following sites: A template engine is a code generator that emits text using templates embedded with actions or data references. Engines generally espouse a model-view pattern in an attempt to separate the data source and template. A container such as a Servlet web server may act as a controller, orchestrating instantiation and controlling model-view interaction. The Java 2.0 release makes StringTemplate much better at code generation while not detracting from the previous HTML generation-centric focus. The simplest way to describe StringTemplate is as a "document" with holes in it where you can stick values. The main distinguishing characteristic between it and the many other engines is StringTemplate's strict enforcement of separation between model (business logic) and view (template). My recent WWW2004 paper, "Enforcing Strict Model-View Separation in Template Engines", was nominated for best paper.

Evaluating StringTemplate

All template engines are trying to solve the same problem so you should be surprised if StringTemplate looked different from other engines. StringTemplate strives to provide sufficient power while limiting certain functionality to enforce strict separation of view from model and controller. Ask not about StringTemplate's expressivity, ask about how well it enforces separation. My 5 years experience building commercial sites indicates that you don't have to sacrifice enforcement of separation to get sufficient power. You might find my slides from the WWW2004 conference a useful quick introduction.

Current release (Java 2.3b7, Python 2.2, C# 2.3b7)

Examples

These are examples for the Java version of StringTemplate.

Check the documentation and the paper.

Showcase

Squarespace.com web publishing site. StringTemplate is used in their production site; templates are exposed to bloggers. Founder Anthony Casalena says,

StringTemplate was PERFECT for this. We needed a system that would: (1) Be extremely fast/simple and (2) ENSURE that users can do nothing to harm our system. Velocity would have been nearly impossible to enforce security with. There were numerous attacks related to invoking classLoaders by referencing getClass on random objects, not to mention that the very paradigm of the templates caused us to write code that did not strictly enforce view vs. controller, further resulting in code we had written that could have been insecure in many cases. It wasn't just impossible to stop users, but also ourselves in many cases.

Publications

The following publication was nominated for

best paper

at WWW 2004 conference: Enforcing Strict Model-View Separation in Template Engines. Here is the abstract:
The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development. This situation is due mostly to a lack of formal definition of separation and fear that enforcing separation emasculates a template's power. I show that not only is strict separation a worthy design principle, but that you can enforce separation while providing a potent template engine. I demonstrate my StringTemplate engine, used to build jGuru.com and other commercial sites, at work solving some nontrivial generational tasks.
My goal is to formalize the study of template engines, thus, providing a common nomenclature, a means of classifying template generational power, and a way to leverage interesting results from formal language theory. I classify three types of restricted templates analogous to Chomsky's type 1..3 grammar classes and formally define separation including the rules that embody separation.
Because this paper provides a clear definition of model-view separation, template engine designers may no longer blindly claim enforcement of separation. Moreover, given theoretical arguments and empirical evidence, programmers no longer have an excuse to entangle model-view.

Requires

For the Python version of StringTemplate 2.2 you need at least ANTLR 2.7.6.