Package: pystache
PystacheThis updated fork of Pystache is currently tested on Python 3.8+ and in
Conda, on Linux, Macos, and Windows (Python 2.7 is no longer supported).Pystacheis a Python
implementation ofMustache.
Mustache is a framework-agnostic, logic-free templating system inspired
byctemplateand
et. Like ctemplate, Mustache “emphasizes separating logic from presentation:
it is impossible to embed application logic in this template language.”Themustache(5)man
page provides a good introduction to Mustache’s syntax. For a more
complete (and more current) description of Mustache’s behavior, see the
officialMustache spec.Pystache issemantically versionedand older
versions can still be found onPyPI.
This version of Pystache now passes all tests inversion 1.1.3of the spec.RequirementsPystache is tested with:Python 3.8Python 3.9Python 3.10Python 3.11Conda (py38 and py310)JSON support is needed only for the command-line interface and to run
the spec tests; PyYAML can still be used (see the Develop section).Official support for Python 2 has ended with Pystache version 0.6.0.NoteThis project usessetuptools_scmto generate and maintain the
version file, which only gets included in the sdist/wheel
packages. In a fresh clone, running any of thetoxcommands
should generate the current version file.Quick StartBe sure to get the latest release from either Pypi or Github.Install ItFrom Pypi:$ pip install pystacheOr Github:$ pip install -U pystache -f https://github.com/PennyDreadfulMTG/pystache/releases/And test it:$ pystache-testTo install and test from source (e.g. from GitHub), see the Develop
section.Use ItOpen a python console:>>> import pystache
>>> print(pystache.render('Hi {{person}}!', {'person': 'Mom'}))
Hi Mom!You can also create dedicated view classes to hold your view logic.Here’s your view class (in ../pystache/tests/examples/readme.py):class SayHello(object):
    def to(self):
        return "Pizza"Instantiating like so:>>> from pystache.tests.examples.readme import SayHello
>>> hello = SayHello()Then your template, say_hello.mustache (by default in the same directory
as your class definition):Hello, {{to}}!Pull it together:>>> renderer = pystache.Renderer()
>>> print(renderer.render(hello))
Hello, Pizza!For greater control over rendering (e.g. to specify a custom template
directory), use theRendererclass like above. One can pass
attributes to the Renderer class constructor or set them on a Renderer
instance. To customize template loading on a per-view basis, subclassTemplateSpec. See the docstrings of theRendererclass andTemplateSpecclass for more information.You can also pre-parse a template:>>> parsed = pystache.parse(u"Hey {{#who}}{{.}}!{{/who}}")
>>> print(parsed)
['Hey ', _SectionNode(key='who', index_begin=12, index_end=18, parsed=[_EscapeNode(key='.'), '!'])]And then:>>> print(renderer.render(parsed, {'who': 'Pops'}))
Hey Pops!
>>> print(renderer.render(parsed, {'who': 'you'}))
Hey you!UnicodeThis section describes how Pystache handles unicode, strings, and
encodings.Internally, Pystache usesonly unicode strings(strin Python 3).
For input, Pystache accepts byte strings (bytesin Python 3).
For output, Pystache’s template rendering methods return only unicode.Pystache’sRendererclass supports a number of attributes to control
how Pystache converts byte strings to unicode on input. These include
thefile_encoding,string_encoding, anddecode_errorsattributes.Thefile_encodingattribute is the encoding the renderer uses to
convert to unicode any files read from the file system. Similarly,string_encodingis the encoding the renderer uses to convert any other
byte strings encountered during the rendering process into unicode (e.g.
context values that are encoded byte strings).Thedecode_errorsattribute is what the renderer passes as theerrorsargument to Python’s built-in unicode-decoding function
(str()in Python 3). The valid values for this argument arestrict,ignore, andreplace.Each of these attributes can be set via theRendererclass’s
constructor using a keyword argument of the same name. See the Renderer
class’s docstrings for further details. In addition, thefile_encodingattribute can be controlled on a per-view basis by subclassing theTemplateSpecclass. When not specified explicitly, these attributes
default to values set in Pystache’sdefaultsmodule.DevelopTo test from a source distribution (without installing):$ python test_pystache.pyTo test Pystache with multiple versions of Python (with a single
command!) and different platforms, you can use [tox](https://pypi.python.org/pypi/tox):$ pip install tox
$ tox -e pyTo run tests on multiple versions with coverage, run:$ tox -e py38-linux,py39-linux  # for example(substitute your platform above, eg, macos or windows)The source distribution tests also include doctests and tests from the
Mustache spec. To include tests from the Mustache spec in your test
runs:$ git submodule update --initThe test harness parses the spec’s (more human-readable) yaml files ifPyYAMLis present. Otherwise,
it parses the json files. To install PyYAML:$ pip install pyyaml  # note this is installed automatically by toxOnce the submodule is available, you can run the full test set with:$ tox -e setup -- ext/spec/specsMaking Changes & ContributingWe use thegitchangelogaction to generate our github Release page, as
well as the gitchangelog message format to help it categorize/filter
commits for a tidier release page. Please use the appropriate ACTION
modifiers in any Pull Requests.This repo is alsopre-commitenabled for various linting and format
checks.  The checks run automatically on commit and will fail the
commit (if not clean) with some checks performing simple file corrections.If other checks fail on commit, the failure display should explain the error
types and line numbers. Note you must fix any fatal errors for the
commit to succeed; some errors should be fixed automatically (usegit statusandgit diffto review any changes).Notepylintis the primary check that requires your own input, as well
as a decision as to the appropriate fix action.  You must fix anypylintwarnings (relative to the baseline config score) for the commit to succeed.See the following pages for more information on gitchangelog and pre-commit.generate-changelogpre-commit-configpre-commit-usageYou will need to install pre-commit before contributing any changes;
installing it using your system’s package manager is recommended,
otherwise install with pip into your usual virtual environment using
something like:$ sudo emerge pre-commit  --or--
$ pip install pre-committhen install it into the repo you just cloned:$ git clone https://github.com/PennyDreadfulMTG/pystache
$ cd pystache/
$ pre-commit installIt’s usually a good idea to update the hooks to the latest version:pre-commit autoupdateMailing List (old)There is(was) amailing list. Note that there is a bit of a delay
between posting a message and seeing it appear in the mailing list archive.Credits>>> import pystache
>>> context = { 'author': 'Chris Wanstrath', 'maintainer': 'Chris Jerdonek','refurbisher': 'Steve Arnold', 'new_maintainer': 'Thomas David Baker' }
>>> print(pystache.render("Author: {{author}}\nMaintainer: {{maintainer}}\nRefurbisher: {{refurbisher}}\nNew maintainer: {{new_maintainer}}", context))
Author: Chris Wanstrath
Maintainer: Chris Jerdonek
Refurbisher: Steve Arnold
New maintainer: Thomas David BakerPystache logo byDavid Phillipsis
licensed under aCreative Commons Attribution-ShareAlike 3.0 Unported
License.