-
Notifications
You must be signed in to change notification settings - Fork 422
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.
The test suite requires at least PHPUnit 3.5.
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.
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.
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
.
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.
<?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'));
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.