Skip to main content

ustache (deprecated)

Mustache for Python, backwards-compatible wrapper of mstache.

:warning: Development moved over to mstache,

Installation

$ pip install ustache

Usage

Python:

import ustache

print(ustache.render('Hello {{v}}', {'v': 'World!'}))
# Hello World!

Command line:

$ ustache -j data.json -o output.html template.mustache

Highlights

  • The fastest pure-python Mustache implementation to this date.
  • Command line interface.
  • Spec compliant, but also highly compatible with Mustache.js.
  • Small codebase, efficiently rendering to str or bytes, supporting streaming.
  • Customizable (property getter, partial resolver, and stringify, escape and lambda render functions).
  • Customizable template caching, with an optional memory-efficient mode (see xxhash optional dependency below).
  • No dynamic code generation, jit and transpiler friendly.

Considerations

For inter-compatibility with JavaScript (especially Mustache.js, enabling client-side rendering with the same templates), ustache exposes some atypical behavior:

  • Mustache blocks stick to JavaScript falseness (__bool__ is not honored): None, False, 0, nan, and empty sequences (including strings) are taken as falsy, while everything else (including empty mappings) will be considered truthy (Mustache.js Boolean and empty Array handling).
  • Mustache blocks receiving any iterable other than mappings and strings will result on a loop (Mustache.js Array handling).
  • Non-mapping sized objects will expose a virtual length property (JavaScript Array.length emulation). Customizable via getter parameter.
  • Mapping keys containing dot (.) or whitespace ( ) are unreachable, (Mustache.js property name limitation). Customizable via getter parameter.
  • Sequence elements are accessible by positive index in the same way mapping integer-keyed items are also accessible when no string key conflicts, as properties (JavaScript Object emulation). Customizable via getter parameter.

Optional dependencies

For minimalism and portability, ustache has no hard dependencies, while still supporting some libraries for added functionality:

  • xxhash will be used, if available, to avoid storing the whole template data as part of the template cache, dramatically reducing its memory footprint in many situations.

Optional but generally recommended dependencies can be easily installed all at once using ustache optional extra target:

$ pip install ustache[optional]

Syntax

Check out the mustache(5) manual.

For quick reference, here is a quick overview of the Mustache syntax.

Template (template.mustache):

{{!comment}}
<ul>
{{#object}}<li>{{property}}</li>{{/object}}
{{^object}}<li>As <b>object</b> is truthy, this won't be shown</li>{{/object}}
{{^null}}<li><b>null</b> is falsy</li>{{/null}}
{{#array}}<li>{{property}}</li>
{{/array}}
{{^array}}<li>Array isn't empty, this won't be shown.</li>{{/array}}
{{#empty_array}}<li>Empty Array, this won't be shown</li>{{/empty_array}}
{{^empty_array}}<li>empty_array is empty</li>{{/empty_array}}
{{&unescaped_html}}
</ul>

Data (data.json):

{
  "object": {
    "property": "Object property value"
  },
  "null": null,
  "array": [
    {"property": "Array item1 property"},
    {"property": "Array item2 property"},
    {"property": "Array item3 property"}
  ],
  "empty_array": [],
  "unescaped_html": "<li>this is unescaped html</li>"
}

Command:

$ ustache -j data.json -o output.html template.mustache

Output:

<ul>
<li>Object property value</li>
<li><b>null</b> is falsy</li>
<li>Array item1 property</li>
<li>Array item2 property</li>
<li>Array item3 property</li>
<li>empty_array is empty</li>
<li>this is unescaped html</li>
</ul>

Metadata

Release files for ustache 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ustache 0.1.6
File Size Uploaded
ustache-0.1.6.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ustache 0.1.6
File Interpreter ABI Platform
ustache-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / ustache-0.1.6.tar.gz

Download URL ustache-0.1.6.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ad00bea46d89a1c8386650e694180665138b10dd8ae1ddd32ae5c6e67531c908
BLAKE2b-256 checksum
How to use checksums
2f613bb1a3d8a7ca9db719240b57341ecbe50e43b8e01c9f16bcce3d564ea8ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.6

Release files / ustache-0.1.6-py3-none-any.whl

Download URL ustache-0.1.6-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f8bec2478abd8a779a2b596dd1c9074b6a18d6f4fecfc2f9dedc24104783e1f
BLAKE2b-256 checksum
How to use checksums
8c5babb369b53317b034a69d4d3668395a0a01e29dd188910983d116d84f3fb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page