Skip to content

fredericksilva/python-mustache

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mustache templating for Python

What?

A recursive descent parser for the mustache templating language.

Install: pip install mustache or easy_install mustache.

Why?

I wrote this to improve my Python skills and learn about recursive descent. I guess it's something that every young programmer needs to do :)

I also wanted to provide a nice way to render templates that didn't require instantiating special classes, or much "magic" in the lookup of templates. I'm not a huge fan of the pystache API.

Compare the mustache way:

from mustache import template

@template('static/templates/index.mustache')
def render_index(name):
    return {'name' : name}

@template('static/other_templates_folder/index2.mustache')
def render_index2(age):
    return {'age' : age}

with the pystache way:

from pystache import Renderer

class Index(object):
    def __init__(self, name):
        self.name = name

def render_index(name):
    return Renderer(search_dirs='t1').render(Index(name))

class Index2(object):
    def __init__(self, age):
        self.age = age

def render_index2(age):
    return Renderer(search_dirs='t2').render(Index2(age))

A nice bonus to the API is that it plays well with bottle, flask, and other micro web frameworks. For example, here's how to use mustache with bottle to show a web page that tells you the current year (useful for time travelers):

wsgi.py:

import time
from bottle import Bottle
from mustache import template

app = Bottle()

@app.get('/')
@template('templates/index.mustache')
def index():
    now = time.time()
    return {'year' : now.tm_year}

templates/index.mustache:

The year is {{year}}!

An important disclaimer: the pystache code works really well and is very clean and well-written. The people who have worked on it have done a great job, and I've learned a lot diving through their code. In fact, I first started this project as a fork of pystache!

Documentation

Coming soon! Sorry!

Testing

As of March 2 2013 this library passes every test in the offical mustache spec, which is included as a git submodule.

To run the test suite:

  • Clone the git repo.
  • From the root, run:
    • git submodule init
    • git submodule update
    • nosetests

Etc.

  • Only depends on the stdlib.
  • Does not support streaming.
  • No command-line utility.
  • Written as a learning experience.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published