Skip to content
bobthecow edited this page Jan 12, 2013 · 39 revisions

Mustache is a simple, logic-less template engine.

We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values.

Prerequisites

Mustache.php requires at least PHP 5.2.x to run.

The test suite requires at least PHPUnit 3.5.

Installation

Use Composer. Add mustache/mustache to your project's composer.json:

{
    "require": {
        "mustache/mustache": "2.0.*"
    }
}

If that doesn't work for you, download the latest release as a zip, or check out the Git repo as a submodule to your project.

Basic usage

Setup

First, you'll need an autoloader.

  • If you installed Mustache using Composer, your Composer autoloader will handle loading classes.

  • If not, you can add Mustache to your PSR-0 compatible autoloader.

  • Otherwise, you will need to register the Mustache autoloader:

    <?php
    require '/path/to/mustache/src/Mustache/Autoloader.php';
    Mustache_Autoloader::register();

There's no step two, really.

What are you waiting for?

<?php
$m = new Mustache_Engine;
echo $m->render('Hello, {{planet}}!', array('planet' => 'World')); // "Hello, world!"

Constructor options

template_class_prefix

The class prefix for compiled templates. Defaults to __Mustache_.

cache

A cache directory for compiled templates. Mustache will not cache templates unless this is set.

cache_file_mode

Override default permissions for cache files. Defaults to using the system-defined umask. It is strongly recommended that you configure your umask properly rather than overriding permissions here.

loader

A Mustache template loader instance. Uses a StringLoader if not specified.

partials_loader

A Mustache loader instance for partials. If none is specified, defaults to a StringLoader for the supplied partials option, if present, and falls back to the default loader.

partials

An array of Mustache partials. Useful for quick-and-dirty string template loading, but not as efficient or lazy as a Filesystem (or database) loader.

helpers

An array of 'helpers'. Helpers can be global variables or objects, closures (e.g. for higher order sections), or any other valid Mustache context value. They will be prepended to the context stack, so they will be available in any template loaded by this Mustache instance.

escape

An 'escape' callback, responsible for escaping double-mustache variables. Defaults to htmlspecialchars.

charset

Character set for htmlspecialchars. Defaults to UTF-8.

logger

A Mustache logger instance. No logging will occur unless this is set. Using a PSR-3 compatible logging library—such as Monolog—is highly recommended. A simple stream logger implementation is available as well.

Using all these options

<?php

require '/path/to/mustache/src/Mustache/Autoloader.php';
Mustache_Autoloader::register();

$mustache = new Mustache_Engine(array(
    'template_class_prefix' => '__MyTemplates_',
    'cache' => dirname(__FILE__).'/tmp/cache/mustache',
    'cache_file_mode' => 0666, // Please, configure your umask instead of doing this :)
    'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'),
    'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views/partials'),
    'helpers' => array('i18n' => function($text) {
        // do something translatey here...
    }),
    'escape' => function($value) {
        return htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
    },
    'charset' => 'ISO-8859-1',
    'logger' => new Mustache_StreamLogger('php://stderr'),
));

$tpl = $mustache->loadTemplate('foo'); // loads __DIR__.'/views/foo.mustache';
echo $tpl->render(array('bar' => 'baz'));

Further documentation

Clone this wiki locally