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.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: moosetash-1.0.1.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/24.5.0

File hashes

Hashes for moosetash-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3b6f16f0497b112108377164d3c7baca35e42c3d7e386468da65ad1d1df419d4
MD5 8556513fb7c970270df93be75d906c0e
BLAKE2b-256 0bd7dc26ce984d2ee548d7e2e95fa5601f9b0f8ff6ae1e59aaccc1f0f35378d5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: moosetash-1.0.1-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/24.5.0

File hashes

Hashes for moosetash-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d2d5405558b7e62163c502718a135bb66f5c085c44d9b903f2ff7f5e89b00eaa
MD5 e03e91445b4001e603801e510d1cbee1
BLAKE2b-256 bf3a4407ee0386c9f8fe5ed2c3b39b61c4f744fdf33213e86a5ec5c5aac8f93d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page