pystache.renderer module

This module provides a Renderer class to render templates.

class pystache.renderer.Renderer(file_encoding=None, string_encoding=None, decode_errors=None, search_dirs=None, file_extension=None, escape=None, partials=None, missing_tags=None)[source]

Bases: object

A class for rendering mustache templates.

This class supports several rendering options which are described in the constructor’s docstring. Other behavior can be customized by subclassing this class.

For example, one can pass a string-string dictionary to the constructor to bypass loading partials from the file system:

>>> partials = {'partial': 'Hello, {{thing}}!'}
>>> renderer = Renderer(partials=partials)
>>> # We apply print to make the test work in Python 3 after 2to3.
>>> print(renderer.render('{{>partial}}', {'thing': 'world'}))
Hello, world!

To customize string coercion (e.g. to render False values as ''), one can subclass this class. For example:

class MyRenderer(Renderer):
    def str_coerce(self, val):
        if not val:
            return ''
        else:
            return str(val)
property context

Return the current rendering context [experimental].

load_template(template_name)[source]

Load a template by name from the file system.

render(template, *context, **kwargs)[source]

Render the given template string, view template, or parsed template.

Returns a unicode string.

Prior to rendering, this method will convert a template that is a byte string (type str in Python 2) to unicode using the string_encoding and decode_errors attributes. See the constructor docstring for more information.

Arguments:

template:

a template string that is unicode or a byte string, a ParsedTemplate instance, or another object instance. In the final case, the function first looks for the template associated to the object by calling this class’s get_associated_template() method. The rendering process also uses the passed object as the first element of the context stack when rendering.

context:

zero or more dictionaries, ContextStack instances, or objects with which to populate the initial context stack. None arguments are skipped. Items in the context list are added to the context stack in order so that later items in the argument list take precedence over earlier items.

kwargs:

additional key-value data to add to the context stack. As these arguments appear after all items in the context list, in the case of key conflicts these values take precedence over all items in the context list.

render_name(template_name, *context, **kwargs)[source]

Render the template with the given name using the given context.

See the render() docstring for more information.

render_path(template_path, *context, **kwargs)[source]

Render the template at the given path using the given context.

Read the render() docstring for more information.

str(b, encoding=None)[source]

Convert a byte string to unicode, using string_encoding and decode_errors.

Arguments:

b: a byte string.

encoding: the name of an encoding. Defaults to the string_encoding

attribute for this instance.

Raises:

TypeError: Because this method calls Python’s built-in unicode()

function, this method raises the following exception if the given string is already unicode:

TypeError: decoding Unicode is not supported

str_coerce(val)[source]

Coerce a non-string value to a string.

This method is called whenever a non-string is encountered during the rendering process when a string is needed (e.g. if a context value for string interpolation is not a string). To customize string coercion, you can override this method.