pystache package

Subpackages

Submodules

Module contents

TODO: add a docstring.

class pystache.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.

class pystache.TemplateSpec[source]

Bases: object

A mixin or interface for specifying custom template information.

The “spec” in TemplateSpec can be taken to mean that the template information is either “specified” or “special.”

A view should subclass this class only if customized template loading is needed. The following attributes allow one to customize/override template information on a per view basis. A None value means to use default behavior for that value and perform no customization. All attributes are initialized to None.

Attributes:

template: the template as a string.

template_encoding: the encoding used by the template.

template_extension: the template file extension. Defaults to “mustache”.

Pass False for no extension (i.e. extensionless template files).

template_name: the name of the template.

template_path: absolute path to the template.

template_rel_directory: the directory containing the template file,

relative to the directory containing the module defining the class.

template_rel_path: the path to the template file, relative to the

directory containing the module defining the class.

template = None
template_encoding = None
template_extension = None
template_name = None
template_path = None
template_rel_directory = None
template_rel_path = None
pystache.parse(template, delimiters=None)[source]

Parse a unicode template string and return a ParsedTemplate instance.

Arguments:

template: a unicode template string.

delimiters: a 2-tuple of delimiters. Defaults to the package default.

Examples:

>>> parsed = parse("Hey {{#who}}{{name}}!{{/who}}")
>>> print(str(parsed).replace('u', ''))  # This is an old hack.
['Hey ', _SectionNode(key='who', index_begin=12, index_end=21, parsed=[_EscapeNode(key='name'), '!'])]
pystache.render(template, context=None, **kwargs)[source]

Return the given template string rendered using the given context.