Chevron Mustache Templating
Chevron is a lightweight, pure-Python implementation of the Mustache templating language. It provides a simple API for rendering templates using dictionaries or objects as data contexts. It adheres closely to the Mustache specification. The current version is 0.14.0, and it generally follows an as-needed release cadence for bug fixes and minor improvements, with long periods of stability.
Warnings
- breaking Python 2 support was dropped in Chevron version 0.13.0. Attempts to use chevron on Python 2.x environments will result in errors.
- breaking The mechanism for resolving partials changed significantly in version 0.11.0. The `path` parameter for specifying partial locations was renamed to `partials_path` and partials are now resolved via a template loader.
- gotcha Mustache's definition of 'falsy' for sections (e.g., `{{#section}}`) differs from Python's default behavior. In Mustache, `0` (the number zero) is considered 'truthy' for sections, while an empty list `[]` is 'falsy'. This can be counter-intuitive for Python developers used to `bool(0)` being `False`.
Install
-
pip install chevron
Imports
- render
import chevron output = chevron.render(template=..., data=...)
Quickstart
import chevron
template_string = 'Hello, {{name}}! You are {{age}} years old.'
data_context = {'name': 'World', 'age': 42}
rendered_output = chevron.render(template=template_string, data=data_context)
print(rendered_output)
# Example with a list and conditional section
template_list = 'Users:\n{{#users}}- {{name}} (ID: {{id}})\n{{/users}}'
data_list = {'users': [{'name': 'Alice', 'id': 1}, {'name': 'Bob', 'id': 2}] }
rendered_list = chevron.render(template=template_list, data=data_list)
print(rendered_list)