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.
Metadata
Release files for moosetash 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| moosetash-1.0.1.tar.gz | 9.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| moosetash-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.2 kB
Release files / moosetash-1.0.1.tar.gz
| Download URL | moosetash-1.0.1.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b6f16f0497b112108377164d3c7baca35e42c3d7e386468da65ad1d1df419d4
|
|
BLAKE2b-256 checksum How to use checksums |
0bd7dc26ce984d2ee548d7e2e95fa5601f9b0f8ff6ae1e59aaccc1f0f35378d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.3 Darwin/24.5.0
|
Release files / moosetash-1.0.1-py3-none-any.whl
| Download URL | moosetash-1.0.1-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d2d5405558b7e62163c502718a135bb66f5c085c44d9b903f2ff7f5e89b00eaa
|
|
BLAKE2b-256 checksum How to use checksums |
bf3a4407ee0386c9f8fe5ed2c3b39b61c4f744fdf33213e86a5ec5c5aac8f93d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.3 Darwin/24.5.0
|