jbboehr / php-mustache

Mustache PHP Extension
MIT License
56 stars 22 forks source link
c-plus-plus mustache php php-extension php-mustache

php-mustache

GitHub Build Status Coverage Status Software License

C++ implementation of Mustache as a PHP extension.

Features

All features of Mustache are supported EXCEPT:

Installation

Linux/OSX

Source

Prerequisite packages are:

git clone git://github.com/jbboehr/php-mustache.git --recursive
cd php-mustache
phpize
./configure --enable-mustache
make
sudo make install

Add the extension to your php.ini:

echo extension=mustache.so | tee -a /path/to/your/php.ini

Fedora/RHEL/CentOS

RPM packages of the extension are available in Remi's repository.

Fedora (change 24 to match your Fedora version)

dnf install https://rpms.remirepo.net/fedora/remi-release-24.rpm
dnf install --enablerepo=remi php-pecl-mustache

RHEL/CentOS (for default PHP in base repository)

yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install php-pecl-mustache

Nix/NixOS

nix-env -i -f https://github.com/jbboehr/php-mustache/archive/master.tar.gz

Windows

See Build your own PHP on Windows. You may need to add msinttypes (export) to your include directory.

Usage

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.

See also: template loader example

Credits

License

The MIT License (MIT). Please see License File for more information.