jbboehr / php-mustache
Mustache templating language extension for PHP
Package info
github.com/jbboehr/php-mustache
Type:php-ext
Ext name:ext-mustache
pkg:composer/jbboehr/php-mustache
Requires
- php: >=8.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-08 05:30:40 UTC
README
C++ implementation of Mustache as a PHP extension.
Upgrading from 0.9.x? Read the migration guide before updating.
Installation
PIE
PIE requires PHP 8.1 or newer to run, but it can install the extension for PHP 8.0. Install libmustache 0.6.0 or newer, then run:
pie install jbboehr/php-mustache
If libmustache is installed under a non-standard prefix, pass it explicitly:
pie install jbboehr/php-mustache --with-libmustache=/path/to/prefix
Source
Requirements:
- PHP 8.0 or newer, including the development headers and tools
- A C++17 compiler
- GNU Make, Automake, Autoconf, and Libtool
pkg-configlibmustache0.6.0 or newer
git clone https://github.com/jbboehr/php-mustache.git cd php-mustache phpize ./configure --enable-mustache make make test sudo make install
If libmustache is installed under a non-standard prefix, pass
--with-libmustache=/path/to/prefix to configure.
Add the extension to php.ini:
extension=mustache.so
Nix
Use the flake package to build a PHP environment with the extension enabled:
{ inputs.php-mustache.url = "github:jbboehr/php-mustache"; outputs = {php-mustache, ...}: let system = "x86_64-linux"; mustache = php-mustache.packages.${system}.default; in { packages.${system}.default = mustache.php.buildEnv { extensions = ({enabled, ...}: enabled ++ [mustache]); }; }; }
The default package uses PHP 8.3. Replace default with a matrix package such
as php85-gcc to select another PHP version.
Windows
Windows source builds require the PHP SDK, Visual Studio 2022, CMake, and a static libmustache build. The Windows CI script contains the setup currently used to build the extension. Pre-built PIE DLLs are not currently published, so PIE installation is not available on Windows.
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 the official Mustache manual for template syntax, the PHP API guide for data conversion, lambdas, errors, and limits, and the template loader example for loading templates from files.
MustacheData and MustacheLambdaHelper are final classes and cannot be
subclassed. To add application behavior around converted data, store a
MustacheData instance in your own class and pass the stored MustacheData
instance to Mustache::render(). The extension supplies MustacheLambdaHelper
to section lambdas; use the helper only during its callback.
PHP also rejects ReflectionClass::newInstanceWithoutConstructor() for these
two classes.
Mustache, MustacheTemplate, and MustacheAST remain extensible. The
PHP stub describes their public methods and types.
License
The MIT License (MIT). Please see License File for more information.