Skip to main content

handlebars.js templating for Python.

Project description

Copyright (c) 2012, Canonical Ltd

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 3 only.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <>. GNU Lesser General Public License version 3 (see the file LICENSE).

pybars provides a template system for Python which is compatible with handlebars.js.


Testing Dependencies


For details on the template language see the documentation.

Translating the engine to python required slightly different calling conventions to the JS version:

  • block helpers take (this, options, *args, **kwargs)

  • other helpers take (this, *args, **kwargs)

  • closures in the context take (this, *args, **kwargs)

A template like ‘{{foo bar quux=1}}’ will pass bar as a positional argument and quux as a keyword argument. Keyword arguments have to be non-reserved words in Python. For instance, ‘print’ as a keyword argument will fail.

Templates with literal boolean arguments like ‘{{foo true}}’ will have the argument mapped to Python’s True or False as appropriate.

For efficiency, rather that passing strings round, pybars passes a subclass of list (‘liststr’) which has a __unicode__ implementation that returns u””.join(self). Template helpers can return any of list, tuple, unicode or liststr instances. liststr exists to avoid quadratic overheads in string processing during template rendering. Helpers that are in inner loops should return list or liststr for the same reason.

NOTE The liststr takes the position of SafeString in the js implementation: when returning a liststr it will not be escaped, even in a regular {{}} expansion.

The ‘data’ facility from the JS implementation has not been ported at this point, if there is demand for it it would be quite easy to add. Similarly the stringParams feature has not been ported - quote anything you wish to force to a string in a helper call.

Typical usage:

  • Grab a compiler:

    >>> from pybars import Compiler
    >>> compiler = Compiler()
  • Register any extensions you need:

    >>> def _list(this, options, items):
    ...     result = [u'<ul>']
    ...     for thing in items:
    ...         result.append(u'<li>')
    ...         result.extend(options['fn'](thing))
    ...         result.append(u'</li>')
    ...     result.append(u'</ul>')
    ...     return result
    >>> compiler.register_helper(u'list', _list)
  • And compile your template:

    >>> source = u"{{#list people}}{{firstName}} {{lastName}}{{/list}}"
    >>> template = compiler.compile(source)
  • You can now render it:

    >>> template({
    ...     'people': [
    ...         {'firstName': "Yehuda", 'lastName': "Katz"},
    ...         {'firstName': "Carl", 'lastName': "Lerche"},
    ...         {'firstName': "Alan", 'lastName': "Johnson"}
    ...    ]})
    <ul><li>Yehuda Katz</li><li>Carl Lerche</li><li>Alan Johnson</li></ul>

More details should be found by reading the API docs (e.g. pydoc pybars).


Either run in an environment with all the dependencies available, or add the working directory to your PYTHONPATH.


Upstream development takes place at To setup a working area for development, if the dependencies are not immediately available, you can use ./ to create bin/buildout, then bin/py to get a python interpreter with the dependencies available.

To run the tests use the runner of your choice, the test suite is pybars.tests.test_suite.

For instance:

$ bin/py -m pybars.tests.test_suite

pybars is testrepository enabled, so you can just do:

$ testr init $ testr run

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybars-0.0.4.tar.gz (18.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page