Skip to main content

mstache, Mustache for Python

Project description

mstache

Mustache for Python.

Documentation: mstache.readthedocs.io

License

MIT License

Installation

pip install mstache

Usage

Python:

import mstache

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

Command line:

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

Highlights

  • The fastest pure-python Mustache implementation to this date.
  • High mustache.js compatibility.
  • Fully spec compliant up to Mustache v1.4.2, including lambda extension (opt-in).
  • Support for chevron-style lambdas (opt-out)
  • Command line interface.
  • Small codebase, efficiently rendering to str or bytes, buffered or stream.
  • Fully customizable behavior for caching, property getter, partial resolver, stringification, escaping, chevron lambda render and mustache block line trimming.
  • Support for binary templates (bytes and keep_lines option).
  • No dynamic code generation, both jit and transpiler friendly.

Considerations

For inter-compatibility with JavaScript (especially mustache.js, to enable client-side rendering the same templates), mstache must behave somewhat atypically for a Python template engine:

  • Mustache blocks stick to JavaScript truthyness:
    • Empty mappings (such as dict) are unconditionally truthy.
    • NaN/-NaN are falsy.
  • Mustache blocks do not iterate mappings nor strings.
  • Sized collections, excluding mappings, will expose a virtual length property, customizable via getter parameter.
  • Mapping keys containing dot (.) or whitespace ( ) are unreachable (common property 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.

An special consideration about the Mustache spec: mstache currently passes most mustache spec tests (plus optional lambdas) but one to ensure mustache.js compatibility.

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:

$ mstache -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>

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

mstache-0.3.0.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

mstache-0.3.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file mstache-0.3.0.tar.gz.

File metadata

  • Download URL: mstache-0.3.0.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for mstache-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1d72368d1cd46a3721f7f7bbe18aa602d19d063ca0d63b164c1ab75c8c410e52
MD5 a6f9437ae658551bc534299ff80550b7
BLAKE2b-256 6c3aa0d22da35675d328ff45c29b35bb0e90b0ea3ff72f7cd7e679e3d383eb6a

See more details on using hashes here.

File details

Details for the file mstache-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mstache-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for mstache-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 17a53df221fe7cab18bf2250db4ed003db5f5e06d84cf41b1c5caeea6da50cf9
MD5 66e31c59075432669661e14b978e7fb5
BLAKE2b-256 6db03742108cf5a2e24edab4e0ac24db9a3858803497e2385270f78a7ed3c525

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