Skip to main content

ustache, Mustache for Python

Project description

ustache

Mustache for Python.

Documentation: ustache.readthedocs.io

Usage

import ustache

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

Command line:

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

Highlights

  • Command line interface.
  • Spec compliant, but also highly compatible with Mustache.js.
  • Fast and small codebase, efficiently rendering to str or bytes, supporting streaming.
  • Customizable (property getter, partial resolver, and stringify, escape and lambda render functions).

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).
  • Sequences 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 if no string key conflicts, as properties (JavaScript Object emulation). Customizable via getter parameter.

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:

$ python -m ustache -j data.json -o 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>

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

ustache-0.1.2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

ustache-0.1.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file ustache-0.1.2.tar.gz.

File metadata

  • Download URL: ustache-0.1.2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

File hashes

Hashes for ustache-0.1.2.tar.gz
Algorithm Hash digest
SHA256 599125593115010c1294a1ffc420c69a0fb0bd4dccf6a42c011308dd9c042f35
MD5 abd0703a5469c96dab30cdf6afb35b3d
BLAKE2b-256 9d7c2de45fda5a5e24b94041017c87302890bf48d91a0c94227164b78a1b8453

See more details on using hashes here.

File details

Details for the file ustache-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ustache-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

File hashes

Hashes for ustache-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0ae5c494a0587082243d107ffe1800d3d28852911f33a4517a011d7ce3e153d4
MD5 fe3c302b55d112a4937785b6ab7dabe1
BLAKE2b-256 f793f24ce8e5865ee162772db85433586d80d5485caa09ad57f9f285a7623023

See more details on using hashes here.

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