Skip to main content

Mustache template renderer

Project description

Moosetash

A Mustache template renderer for Python

Moosetash is a python implementation of the Mustache templating specification.

Usage

Basic Usage

Parsing templates is as easy as passing a template and a variable context to Moosetash's render function:

from moosetash import render


render('Hello {{ variable }}!', {'variable': 'world'})
# Output: Hello world!

Custom Serialisation

By default, variables are serialised through string conversion by calling str. This behaviour can be customised by passing your own serialisation function:

from typing import Any
from datetime import date
from moosetash import render


def date_serializer(value: Any) -> str:
    if isinstance(value, date):
        return value.strftime('%d/%m/%Y')
    return str(value)


render('{{variable}}', {'variable': date(2020, 1, 10)}, serializer=date_serializer)
# Output: '10-01/2020'

Fallback behaviour

By default, Mustache specifies that missing variables or partials are represented as empty strings. You can customise how the renderer handles missing variables or partials.

from moosetash import (
    render,
    missing_variable_raise,
    missing_variable_keep,
)


render('{{missing}}', {})
# Output: ''

render('{{missing}}', {}, missing_variable_handler=missing_variable_raise)
# Raises: moosetash.MissingVariable('missing')

render('{{missing}}', {}, missing_variable_handler=missing_variable_keep)
# Output: '{{missing}}'

def custom_missing_handler(variable_name: str, variable_tag: str) -> str:
    return f'[Missing variable with name {variable_name}]'

render('{{missing}}', {}, missing_variable_handler=custom_missing_handler)
# Output: '[Missing variable with name missing]'

Similarly, for partials:

from moosetash import (
    render,
    missing_partial_raise,
    missing_partial_keep,
)


render('{{>missing}}', {})
# Output: ''

render('{{>missing}}', {}, missing_partial_handler=missing_partial_raise)
# Raises: moosetash.MissingPartial('missing')

render('{{>missing}}', {}, missing_partial_handler=missing_partial_keep)
# Output: '{{missing}}'

def custom_missing_handler(partial_name: str, partial_tag: str) -> str:
    return f'[Missing partial with name {partial_name}]'

render('{{>missing}}', {}, missing_partial_handler=custom_missing_handler)
# Output: '[Missing partial with name missing]'

Contributing

We welcome any contributions to this repo.

Set up locally

Moostash uses poetry for package management. You can install locally:

poetry init

Tests

Moostash uses pytest for tests, and aims for 100% coverage. To run all tests with coverage output:

poetry run pytest --cov=moosetash

A significant number of tests are loaded directly from the mustache spec (see the spec folder.)

Formatting

Moostash uses black and isort for automated formatting. Both of these should be run through poetry, and are configured in pyproject.toml:

poetry run black .
poetry run isort .

Typing

Moosetash is a typed python library, and aims for comprehensive type coverage. You can check the types using mypy:

poetry run mypy .

Benchmarks

This repo contains a series of benchmarks that can be run against different other Python mustache renderers, for comparing performance:

  • Chevron is currently the recommended Python implementation, and is generally fast and efficient.

You can run the benchmark suite using poetry:

poetry run benchmark

TODO

  • Support for inheritance
  • Additional benchmarks
  • Performance analysis and improvement

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

moosetash-1.0.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

moosetash-1.0.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file moosetash-1.0.0.tar.gz.

File metadata

  • Download URL: moosetash-1.0.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Darwin/23.5.0

File hashes

Hashes for moosetash-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d4fc690a11e68dd77beae41bd8d1b2ae1a4d9a2b45f1ac71569665c4bb691515
MD5 a472e019d9226051656f08287172ae32
BLAKE2b-256 5645a0c0b1242de0e14c46a9617f519342ffc9ba8939251ffb80b1203c1a71d9

See more details on using hashes here.

File details

Details for the file moosetash-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: moosetash-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Darwin/23.5.0

File hashes

Hashes for moosetash-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc809a9f1b809b6b307c7c54a4a3156f1702e484b7979bd61076c2822cc62543
MD5 3a62417edce31862dc568bfca171a65c
BLAKE2b-256 58b2770595d2767925ab20a7a1e9e2fdbbb8aca7f648c4109c167c6dae767f6d

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