-
Notifications
You must be signed in to change notification settings - Fork 423
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 with nothing, and others with a series of values.
Mustache.php requires at least PHP 5.2.x to run, but works great in 5.3, 5.4, 5.5, 5.6, 7.0, 7.1 and HHVM, too!.
The test suite requires at least PHPUnit 3.5.
Use Composer. Add mustache/mustache
to your project's composer.json
:
{
"require": {
"mustache/mustache": "~2.5"
}
}
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 Mustache Cache instance or 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.
Enable template caching for lambda sections. This is generally not recommended, as lambda sections are often too dynamic to benefit from caching.
A Mustache template loader instance. Uses a StringLoader if not specified.
A Mustache loader instance for partials. If none is specified, defaults to an ArrayLoader for the supplied partials
option, if present, and falls back to the specified template 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
.
The type argument for htmlspecialchars
. Defaults to ENT_COMPAT
, but you may prefer ENT_QUOTES
.
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.
Only treat Closure instances and invokable classes as callable. If true, values like array('ClassName', 'methodName')
and array($classInstance, 'methodName')
, which are traditionally "callable" in PHP, are not called to resolve variables for interpolation or section contexts. This helps protect against arbitrary code execution when user input is passed directly into the template.
This currently defaults to false, but will default to true in v3.0.
Enable pragmas across all templates, regardless of the presence of pragma tags in the individual templates.
Customize the tag delimiters used by this engine instance. Note that overriding here changes the delimiters used to parse all templates and partials loaded by this instance. To override just for a single template, use an inline "change delimiters" tag at the start of the template file: {{=<% %>=}}
.
<?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 :)
'cache_lambda_templates' => true,
'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_Logger_StreamLogger('php://stderr'),
'strict_callables' => true,
'pragmas' => [Mustache_Engine::PRAGMA_FILTERS],
));
$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.