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.
Common errors
-
ModuleNotFoundError: No module named 'chevron'
cause The `chevron` library has not been installed in your Python environment or the environment where you are running your code is not the one where `chevron` was installed.fixInstall the library using pip: `pip install chevron` -
KeyError: 'some_missing_key'
cause You are trying to render a template that references a variable (e.g., `{{ some_missing_key }}`) that is not present in the data dictionary provided to `chevron.render()` or is misspelled.fixEnsure that all variables referenced in your Mustache template are present as keys in the data dictionary passed to `chevron.render()`. For example, if your template has `{{ name }}`, your data should be `{'name': 'World'}`. -
SyntaxError: unmatched section tag 'wrong_section' in template '{{# section }} oops {{/ wrong_section }}'cause The Mustache template contains an improperly closed section tag. `chevron` enforces strict Mustache specification compliance, and mismatched or unclosed section tags will result in a `SyntaxError` (or `ChevronError`, which `SyntaxError` catches).fixCorrect the closing section tag to match its opening tag. For example, if you open with `{{# section }}`, you must close with `{{/ section }}`. -
No output is printed when running a Python script using `chevron.render()`.
cause The `chevron.render()` function returns the rendered string, but it does not automatically print it to the console when executed as a script. In a REPL, the return value is automatically displayed, but in a script, you must explicitly print it.fixWrap the `chevron.render()` call with a `print()` function: `print(chevron.render('Hello, {{name}}!', {'name': 'World'}))` -
AttributeError: 'module' object has no attribute 'render'
cause This error typically occurs if you've incorrectly imported `chevron` or if a different module named `chevron` is being imported instead of the templating library, or if you're trying to call `render` on the module itself rather than an instantiated object (which is not how `chevron` works).fixEnsure you are importing `chevron` correctly as `import chevron` and then calling its `render` function directly as `chevron.render(...)`. Verify no other files or packages named `chevron.py` or `chevron` exist in your Python path that might conflict with the intended library.
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)