{"id":2231,"library":"pystache","title":"Pystache","description":"Pystache is a Python implementation of the Mustache templating language, emphasizing a logic-free approach to separate presentation from application logic. It fully supports the Mustache spec and is currently maintained as a community fork. The latest stable release is 0.6.8, released on March 18, 2025.","status":"active","version":"0.6.8","language":"en","source_language":"en","source_url":"https://github.com/PennyDreadfulMTG/pystache.git","tags":["templating","mustache","logic-less templates"],"install":[{"cmd":"pip install pystache","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Optional dependency for running spec tests and command-line interface, especially when parsing YAML versions of the Mustache spec.","package":"PyYAML","optional":true}],"imports":[{"symbol":"render","correct":"import pystache\npystache.render(template, context)"},{"symbol":"Renderer","correct":"from pystache import Renderer\nrenderer = Renderer()"},{"note":"Used for advanced customization of template loading on a per-view basis.","symbol":"TemplateSpec","correct":"from pystache.template_spec import TemplateSpec"},{"note":"The 'View' class was removed in Pystache 0.5.0; use plain Python objects or 'TemplateSpec' with 'Renderer' instead.","wrong":"from pystache import View","symbol":"View"}],"quickstart":{"code":"import pystache\n\n# Simplest rendering\ntemplate = \"Hello, {{person}}!\"\ncontext = {\"person\": \"World\"}\nprint(pystache.render(template, context))\n\n# Using Renderer for more control (e.g., custom template directories)\nrenderer = pystache.Renderer()\nprint(renderer.render(template, context))\n\n# Using a dedicated view class (no inheritance needed since 0.5.0)\nclass SayHello:\n    def to(self):\n        return \"Pizza\"\n\nhello_view = SayHello()\ntemplate_view = \"Hello, {{to}}!\"\nprint(renderer.render(template_view, hello_view))\n","lang":"python","description":"This quickstart demonstrates the basic usage of Pystache with direct rendering, using the Renderer class for more control, and integrating with a simple Python object as a 'view' for data."},"warnings":[{"fix":"Migrate from `View` to plain Python objects or `TemplateSpec` with `Renderer`. Adjust template rendering calls to use `pystache.render()` or `Renderer` instances.","message":"Pystache 0.5.0 introduced significant API changes, including the removal of the `View` class and replacement of the `Template` class with `Renderer`. Code written for pre-0.5.0 versions will require updates.","severity":"breaking","affected_versions":"<0.5.0"},{"fix":"Explicitly set a custom escape function via `Renderer(escape=my_escape_func)` if consistent escaping behavior, especially for single quotes, is critical across Python versions.","message":"The default HTML escape function behaves differently between Python 2 (not escaping single quotes with `cgi.escape()`) and Python 3 (escaping single quotes with `html.escape()`). This can lead to subtle rendering differences or unexpected security issues if not accounted for.","severity":"gotcha","affected_versions":"All versions (behavior difference across Python major versions)"},{"fix":"Configure `Renderer` with appropriate `file_encoding`, `string_encoding`, and `decode_errors` parameters, or ensure all template inputs are explicitly unicode strings (Python 3 `str`).","message":"Pystache internally uses unicode. When passing byte strings as input (e.g., from files or network), explicit encoding attributes (`file_encoding`, `string_encoding`, `decode_errors`) on the `Renderer` might be necessary to control conversion and prevent `UnicodeDecodeError` or incorrect rendering.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always install via `pip` which handles the conversion automatically, or ensure manual `2to3` conversion if importing from a source checkout.","message":"If installing from source, Pystache's original code was written for Python 2 and must be converted (e.g., by `2to3`) for Python 3 environments. Importing directly from an unconverted Python 2 source directory in a Python 3 environment will result in a `SyntaxError`.","severity":"gotcha","affected_versions":"All versions when installing from source for Python 3"},{"fix":"Understand the Mustache spec's definition of implicit iterators. Use `{{.}}` primarily within sections iterating over primitive values where you want to render the value itself. For objects, refer to specific properties (e.g., `{{name}}`).","message":"The `{{.}}` tag (implicit iterator) is supported for accessing the current value within a section context (e.g., when iterating over a list of simple values). While useful, its behavior is not always intuitive and was a later addition to the Mustache spec.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}