Ibis

A lightweight template engine in Python.

Quickstart


A template starts life as a simple string containing template markup. We use this string to initialize a Template object:

>>> import ibis
>>> template = ibis.Template('{{foo}} and {{bar}}')

The Template class takes the string and compiles it into a template object. Compilation only needs to happen once; the template can then be rendered multiple times by calling its render() method with a dictionary of key-value pairs:

>>> template.render({'foo': 'ham', 'bar': 'eggs'})
'ham and eggs'

>>> template.render({'foo': 1, 'bar': 2})
'1 and 2'

In practice you'll most likely store your templates in the form of text files. You can handle the process of loading these files, compiling them into templates, and storing the resulting Template objects yourself, or you can take advantage of the built-in FileLoader class to take care of the details for you.

A FileLoader instance is initialized with a path to a root template directory:

import ibis
loader = ibis.loaders.FileLoader('/path/to/template/directory')

Calling the loader with a template name (a string interpreted as a path to a text file stored in the root directory) will return the corresponding template object:

template = loader('template.txt')

The loader compiles the templates once and caches them in memory for future lookups. You can learn more about using template loaders here.