, (*1)
Packfire Mustache is a lightweight Mustache implementation for Packfire. Prior to the adoptation of Mustache, Packfire has used a simple token-replace templating engine that uses single curly braces to indicate tokens., (*2)
<h1>
{title}
</h1>
<p>{message}</p>
However, the simple templating parser is way too primitive and does not support nesting and listing like Mustache does. Hence, Packfire adopted the Mustache templating engine as its default templating engine., (*3)
However, the PHP Mustache implementation by bobthecow was found to be too heavy and thus I decided to come up with our own. Mustache has been tested against the provided spec tests., (*4)
Packfire Mustache uses double curly braces to indicate tokens, supports escaping by default and supports nested block tokens:, (*5)
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}
To find out more about how Packfire Mustache works you can refer to the original Mustache manual as Mustache is designed to be platform-independent and cross-platform compatible., (*6)
Installation
Packfire Mustache can be installed via Composer:, (*7)
{
"require": {
"packfire/mustache": "1.0.*"
}
}
Then run the Composer installation command:, (*8)
$ composer install
Usage
A quick example:, (*9)
use Packfire\Template\Mustache\Mustache;
$m = new Mustache('Hello {{planet}}!');
echo $m->parameters(array('planet' => 'World'))->render();
// "Hello World!"
And a more in-depth example--this is the canonical Mustache template:, (*10)
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}
Along with the associated Mustache class:, (*11)
use Packfire\Template\Mustache\Mustache;
class Chris extends Mustache {
public $name = "Chris";
public $value = 10000;
public function taxed_value() {
return $this->value - ($this->value * 0.4);
}
public $in_ca = true;
}
Render it like so:, (*12)
$chris = new Chris;
echo $chris->template($template)->render();
Here's the same thing, a different way:, (*13)
Create a view object--which could also be an associative array, but those don't do functions quite as well:, (*14)
class Chris {
public $name = "Chris";
public $value = 10000;
public function taxed_value() {
return $this->value - ($this->value * 0.4);
}
public $in_ca = true;
}
And render it:, (*15)
use Packfire\Template\Mustache\Mustache;
$chris = new Chris();
$m = new Mustache();
echo $m->template($template)->parameters($chris)->render();