Mustache.php

January 29, 2026 ยท View on GitHub

A Mustache implementation in PHP.

Package version Monthly downloads

Installation

composer require mustache/mustache

Usage

A quick example:

<?php
$m = new \Mustache\Engine(['entity_flags' => ENT_QUOTES]);
echo $m->render('Hello {{planet}}', ['planet' => 'World!']); // "Hello World!"

And a more in-depth example -- this is the canonical Mustache template:

<?php
$template = '
Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}';

Create a view "context" object -- which could also be an associative array, but those don't do functions quite as well:

<?php
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:

<?php
$m = new \Mustache\Engine(['entity_flags' => ENT_QUOTES]);
$chris = new \Chris;
echo $m->render($template, $chris);

Note: we recommend using ENT_QUOTES as a default of entity_flags to decrease the chance of Cross-site scripting vulnerability.

And That's Not All!

Read the Mustache.php documentation for more information.

Upgrading from v2.x

Mustache.php v3.x drops support for PHP 5.2โ€“5.5, but is otherwise backwards compatible with v2.x.

To ease the transition, previous behavior can be preserved via configuration:

  • The strict_callables config option now defaults to true. Lambda sections should use closures or callable objects. To continue supporting array-style callables for lambda sections (e.g. [$this, 'foo']), set strict_callables to false.
  • A context shadowing bug from v2.x has been fixed, but if you depend on the previous buggy behavior you can preserve it via the buggy_property_shadowing config option.
  • By default the return value of higher-order sections that are rendered via the lambda helper will no longer be double-rendered. To preserve the previous behavior, set double_render_lambdas to true. This is not recommended.

In order to maintain a wide PHP version support range, there are minor changes to a few interfaces, which you might need to handle if you extend Mustache (see c0453be).

See Also