C++ implementation of Mustache as a PHP extension. See Mustache
All features of Mustache are supported EXCEPT:
- Lambda functions
- Whitespace rules. All whitespace is kept as it is in the input template.
Install libmustache
Ubuntu:
sudo apt-get install git-core php5-dev
git clone git://github.com/jbboehr/php-mustache.git --recursive
cd php-mustache
phpize
./configure --enable-mustache
make
sudo make install
echo extension=mustache.so | sudo tee /etc/php5/conf.d/mustache.ini
Windows:
- Get a Mustache Windows Build
- Place into your PHP extension directory and add
extension=php_mustache.dll
to your php.ini
Note: these pass all of the tests, but may or may not be production safe.
Example:
<?php
$mustache = new Mustache();
$tmpl = <<<EOF
Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
EOF;
$data = array(
'name' => 'John',
'value' => 10000,
'taxed_value' => 10000 * 0.6,
'in_ca' => true,
);
$partials = array();
echo $mustache->render($tmpl, $data, $partials);
Produces:
Hello John
You have just won 10000 dollars!
Well, 6000 dollars, after taxes.
Example 2:
<?php
$mustache = new Mustache();
$tmpl = $mustache->compile('{{var}}');
$data = new MustacheData(array('var' => 'val'));
for( $i = 0; $i < 5; $i++ ) {
echo $mustache->execute($tmpl, $data);
}
Produces:
valvalvalvalval
- Fix whitespace non-conformity
- Cache compiled templates in memory
- Need to add msinttypes to your include directory.
- See Build your own PHP on Windows.
Note: SetEnv.cmd is not always in the path, mine was in
C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin\SetEnv.cmd
and needed to be modified (modified version is in the win32 folder in this repository) - Place sources into the ext directory (e.g.
C:\php-sdk\php53dev\vc9\x86\php5.3-xyz\ext\mustache
) configure --enable-mustache=shared ...
orconfigure --enable-mustache ...
to compile it into PHP. If compiling it as shared, make sure --enable-zts matches the version you have installed.nmake
To run the tests for the shared extension, add
extension=C:\php-sdk\php53\vc9\x86\php5.3-xyz\Release\php_mustache.dll
to
C:\php-sdk\php53\vc9\x86\php5.3-xyz\Release\php.ini
then run nmake test TESTS=ext\mustache\tests\*
(replace Release
with Release_TS
if thread safety is enabled)