Mustache is a framework-agnostic way to render logic-free views that is inspired by ctemplate and et. Like ctemplate, "it emphasizes separating logic from presentation: it is impossible to embed application logic in this template language."
The mustache(5) man page provides a good introduction to Mustache's syntax. For a more complete (and more current) description of Mustache's behavior, see the official Mustache spec.
Pystache is a Python implementation of Mustache. It currently passes all tests in version 1.0.3 of the Mustache spec. Pystache itself is semantically versioned.
Logo: David Phillips
Pystache is currently tested under Python 2.6.
pip install pystache
>>> import pystache >>> pystache.render('Hi {{person}}!', {'person': 'Mom'}) u'Hi Mom!'
You can also create dedicated view classes to hold your view logic.
Here's your view class (in examples/readme.py):
class SayHello(object): def to(self): return "Pizza"
Like so:
>>> from examples.readme import SayHello >>> hello = SayHello()
Then your template, say_hello.mustache:
Hello, {{to}}!
Pull it together:
>>> renderer = pystache.Renderer() >>> renderer.render(hello) u'Hello, Pizza!'
nose works great!
pip install nose cd pystache nosetests
To include tests from the Mustache spec in your test runs:
git submodule init git submodule update
To run all available tests (including doctests):
nosetests --with-doctest --doctest-extension=rst
As of November 2011, there's a mailing list, pystache@librelist.com.
Archive: http://librelist.com/browser/pystache/
Note: There's a bit of a delay in seeing the latest emails appear in the archive.
>>> context = { 'author': 'Chris Wanstrath', 'email': 'chris@ozmm.org' } >>> pystache.render("{{author}} :: {{email}}", context) u'Chris Wanstrath :: chris@ozmm.org'