-
Notifications
You must be signed in to change notification settings - Fork 0
Home
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.
Mustache.php requires at least PHP 5.2.x to run.
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.
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.
<?php
$m = new Mustache_Engine;
echo $m->render('Hello, {{planet}}!', array('planet' => 'World')); // "Hello, world!"
The class prefix for compiled templates. Defaults to __Mustache_
.
A cache directory for compiled templates. Mustache will not cache templates unless this is set.
A Mustache template loader instance. Uses a StringLoader if not specified.
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
.
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.
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.
An 'escape' callback, responsible for escaping double-mustache variables. Defaults to htmlspecialchars
.
Character set for htmlspecialchars
. Defaults to UTF-8
.
<?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',
'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',
);
$tpl = $mustache->loadTemplate('foo'); // loads __DIR__.'/views/foo.mustache';
echo $tpl->render(array('bar' => 'baz'));