Skip to main content

Mustache templating language renderer

Project description

PyPI version Build Status Coverage Status

A python implementation of the mustache templating language.

Why chevron?

I'm glad you asked!

chevron is fast

Chevron runs in less than half the time of pystache (Which is not even up to date on the spec). And in about 70% the time of Stache (A 'trimmed' version of mustache, also not spec compliant).

chevron is pep8

The flake8 command is run by travis to ensure consistency.

chevron is spec compliant

Chevron passes all the unittests provided by the spec (in every version listed below).

If you find a test that chevron does not pass, please report it.

chevron is Python 2 and 3 compatible

Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5, and 3.6 are all tested by travis.

USAGE

Commandline usage: (if installed via pypi)

usage: chevron [-h] [-v] [-d DATA] [-p PARTIALS_PATH] [-e PARTIALS_EXT]
               [-l DEF_LDEL] [-r DEF_RDEL]
               template

positional arguments:
  template              The mustache file

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -d DATA, --data DATA  The json data file
  -p PARTIALS_PATH, --path PARTIALS_PATH
                        The directory where your partials reside
  -e PARTIALS_EXT, --ext PARTIALS_EXT
                        The extension for your mustache partials, 'mustache'
                        by default
  -l DEF_LDEL, --left-delimiter DEF_LDEL
                        The default left delimiter, "{{" by default.
  -r DEF_RDEL, --right-delimiter DEF_RDEL
                        The default right delimiter, "}}" by default.

Python usage with strings

import chevron

chevron.render('Hello, {{ mustache }}!', {'mustache': 'World'})

Python usage with file

import chevron

with open('file.mustache', 'r') as f:
    chevron.render(f, {'mustache': 'World'})

Python usage with unpacking

import chevron

args = {
  'template': 'Hello, {{ mustache }}!',

  'data': {
    'mustache': 'World'
  }
}

chevron.render(**args)

chevron supports partials (via dictionaries)

import chevron

args = {
    'template': 'Hello, {{> thing }}!',

    'partials_dict': {
        'thing': 'World'
    }
}

chevron.render(**args)

chevron supports partials (via the filesystem)

import chevron

args = {
    'template': 'Hello, {{> thing }}!',

    # defaults to .
    'partials_path': 'partials/',

    # defaults to mustache
    'partials_ext': 'ms',
}

# ./partials/thing.ms will be read and rendered
chevron.render(**args)

chevron supports lambdas

import chevron

def first(text, render):
    # return only first occurance of items
    result = render(text)
    return [ x.strip() for x in result.split(" || ") if x.strip() ][0]

def inject_x(text, render):
    # inject data into scope
    return render(text, {'x': 'data'})

args = {
    'template': 'Hello, {{# first}} {{x}} || {{y}} || {{z}} {{/ first}}!  {{# inject_x}} {{x}} {{/ inject_x}}',

    'data': {
        'y': 'foo',
        'z': 'bar',
        'first': first,
        'inject_x': inject_x
    }
}

chevron.render(**args)

INSTALL

  • with git
$ git clone https://github.com/noahmorrison/chevron.git

or using submodules

$ git submodules add https://github.com/noahmorrison/chevron.git

Also available on pypi!

  • with pip
$ pip install chevron

TODO

  • get popular
  • have people complain
  • fix those complaints

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chevron-0.13.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

chevron-0.13.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file chevron-0.13.1.tar.gz.

File metadata

  • Download URL: chevron-0.13.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.3

File hashes

Hashes for chevron-0.13.1.tar.gz
Algorithm Hash digest
SHA256 f95054a8b303268ebf3efd6bdfc8c1b428d3fc92327913b4e236d062ec61c989
MD5 008fd615a646c35f3ba0697722ebc4c4
BLAKE2b-256 2a01efb4ef22ea9b6377392bd5d6af5acbd218100ee7379dbcd8a7322585710d

See more details on using hashes here.

File details

Details for the file chevron-0.13.1-py3-none-any.whl.

File metadata

  • Download URL: chevron-0.13.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.3

File hashes

Hashes for chevron-0.13.1-py3-none-any.whl
Algorithm Hash digest
SHA256 95b0a055ef0ada5eb061d60be64a7f70670b53372ccd221d1b88adf1c41a9094
MD5 0d5373c155486bf05668bb95ebbb2c28
BLAKE2b-256 a31e6ef58385f54c5bbf70b3b451930015aa2d30b3cd5ae324bba4da77c5b889

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page