PHPackages                             xp-forge/mustache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. xp-forge/mustache

ActiveLibrary[Templating &amp; Views](/categories/templating)

xp-forge/mustache
=================

The Mustache template language implemented for the XP Framework

v9.0.1(8mo ago)149.9k↓12.5%[1 PRs](https://github.com/xp-forge/mustache/pulls)1BSD-3-ClausePHPPHP &gt;=7.4.0CI passing

Since Dec 31Pushed 8mo ago2 watchersCompare

[ Source](https://github.com/xp-forge/mustache)[ Packagist](https://packagist.org/packages/xp-forge/mustache)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-forge-mustache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (39)Used By (1)

Mustache
========

[](#mustache)

[![Build status on GitHub](https://github.com/xp-forge/mustache/workflows/Tests/badge.svg)](https://github.com/xp-forge/mustache/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/58ce56f4bd7c1a53fe112622514aff890e6ef7b1877ec41f3d664cca6f0fca81/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f6d757374616368652f76657273696f6e2e737667)](https://packagist.org/packages/xp-forge/mustache)

The [mustache template language](http://mustache.github.io/) implemented for the XP Framework.

```
use com\github\mustache\MustacheEngine;

$transformed= (new MustacheEngine())->render(
  'Hello {{name}}',
  ['name' => 'World']
);
```

Introduction
------------

[](#introduction)

Read the excellent [mustache man-page](http://mustache.github.io/mustache.5.html) for a start.

Features supported
------------------

[](#features-supported)

This implementation supports all standard features of the [current specification](https://github.com/mustache/spec):

- Interpolation: `{{var}}` (`{{&var}}` and triple mustaches for unescaped) and the dot-notation. By default misses will be replaced by empty strings.
- Comments: `{{! comment }}`. Comments will appear in the parse tree but of course will be excluded when rendering.
- Delimiters: `{{=@ @=}}` will change the starting and ending delimiter to the `@` sign. Arbitrary length delimiters are supported.
- Sections: `{{#section}}` as well as inverted sections: `{{^section}}` are supported.
- Partials: `{{> partial}}` will load a template called `partial.mustache` from the template loader.
- Implicit iterators: Written as `{{.}}`, the current loop value will be selected.

The optional *lambdas* module as well as the parent context extension (`../name`) are also supported.

Lambdas
-------

[](#lambdas)

If the value is a closure, it will be invoked and the raw text (no interpolations will have been performed!) will be passed to it:

### Template

[](#template)

```
{{# wrapped }}
  {{ name }} is awesome.
{{/ wrapped }}
```

### Data

[](#data)

```
[
  'name'    => 'Willy',
  'wrapped' => function($text) {
    return ''.$text.'';
  }
];
```

### Output

[](#output)

```
Willy is awesome.
```

### Extended usage

[](#extended-usage)

The `$text` parameter passed is actually a `com.github.mustache.Node` instance but may be treated as text as it overloads the string cast. In order to work with it, the node's `evaluate()` method can be called with the `com.github.mustache.Context` instance given as the second argument:

```
[
  'name'    => 'Willy',
  'wrapped' => function($node, $context) {
    return ''.strtoupper($node->evaluate($context)).'';
  }
]
```

Template loading
----------------

[](#template-loading)

Per default, templates are loaded from the current working directory. This can be changed by passing a template loader instance to the engine:

```
use com\github\mustache\{MustacheEngine, FilesIn};
use io\Folder;

$engine= new MustacheEngine();
$engine->withTemplates(new FilesIn(new Folder('templates')));
$transformed= $engine->transform('hello', ['name' => 'World']);
```

This will load the template stored in the file `templates/hello.mustache`. This template loader will also be used for partials.

Templates can also be loaded from the class loader, use the `com.github.mustache.ResourcesIn` and pass it a class loader instance (e.g. `ClassLoader::getDefault()` to search in all class paths) for this purpose.

Compiled templates
------------------

[](#compiled-templates)

If you wish to apply variables to a template more than once, you can speed that process up by precompiling templates and using them later on:

```
use com\github\mustache\MustacheEngine;

$engine= new MustacheEngine();
$template= $engine->compile($template);

// Later on:
$result1= $engine->evaluate($template, $variables1);
$result2= $engine->evaluate($template, $variables2);
```

Helpers
-------

[](#helpers)

Think of helpers as "omnipresent" context. They are added to the engine instance via `withHelper()` and will be available in any rendering context invoked on that instance.

### Template

[](#template-1)

```
{{# bold }}
  This is {{ location }}!
{{/ bold }}
```

### Call

[](#call)

```
use com\github\mustache\MustacheEngine;

$engine= new MustacheEngine();
$engine->withHelper('bold', function($text) {
  return ''.$text.'';
});
$transformed= $engine->render($template, ['location' => 'Spartaaaaa']);
```

### Output

[](#output-1)

```
This is Spartaaaaa!
```

### Using objects

[](#using-objects)

You can also use instance methods as helpers, e.g.

```
// Declaration
class LocalizationHelpers {
  public function date($list, $context) {
    return $context->lookup($list->nodeAt(0)->name())->toString('d.m.Y');
  }

  public function money($list, $context) {
    // ...
  }
}

// Usage with engine instance
$engine->withHelper('local', new LocalizationHelpers());
```

```
{{#local.date}}{{date}}{{/local.date}}
```

Spec compliance
---------------

[](#spec-compliance)

Whether this implementation is compliant with the official spec can be tested as follows:

```
$ curl -sSL https://github.com/mustache/spec/archive/master.zip -o master.zip
$ unzip master.zip && rm master.zip
$ xp test com.github.mustache.** -a spec-master/specs/
```

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance60

Regular maintenance activity

Popularity29

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 99.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~108 days

Recently: every ~170 days

Total

37

Last Release

254d ago

Major Versions

v4.0.0 → v5.0.02017-06-03

v5.3.3 → v6.0.02020-10-18

v6.1.2 → v7.0.02021-05-02

v7.0.0 → v8.0.02021-10-21

v8.2.0 → v9.0.02025-05-04

PHP version history (5 changes)v1.3.1PHP &gt;=5.4.0

v2.0.0PHP &gt;=5.5.0

v4.0.0PHP &gt;=5.6.0

v6.0.0PHP &gt;=7.0.0

v9.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (368 commits)")[![kiesel](https://avatars.githubusercontent.com/u/127769?v=4)](https://github.com/kiesel "kiesel (1 commits)")

---

Tags

mustachephptemplatesxp-frameworkmodulexp

### Embed Badge

![Health badge](/badges/xp-forge-mustache/health.svg)

```
[![Health](https://phpackages.com/badges/xp-forge-mustache/health.svg)](https://phpackages.com/packages/xp-forge-mustache)
```

###  Alternatives

[kokspflanze/zfc-twig

Laminas/Zend Framework Module that provides a Twig rendering strategy and extensions to render actions or trigger events from your templates

15299.3k4](/packages/kokspflanze-zfc-twig)[oxcom/zend-twig

ZendTwig is a module that integrates the Twig template engine with Zend Framework 3.

19109.7k](/packages/oxcom-zend-twig)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
