PHPackages                             xp-forge/handlebars - 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. xp-forge/handlebars

ActiveLibrary

xp-forge/handlebars
===================

Handlebars for XP Framework

v10.0.1(8mo ago)047.4k↓12.5%[1 issues](https://github.com/xp-forge/handlebars/issues)1BSD-3-ClausePHPPHP &gt;=7.4.0CI passing

Since Jan 10Pushed 8mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (4)Versions (56)Used By (1)

Handlebars for XP Framework
===========================

[](#handlebars-for-xp-framework)

[![Build status on GitHub](https://github.com/xp-forge/handlebars/workflows/Tests/badge.svg)](https://github.com/xp-forge/handlebars/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/44cc74556115140cbc2b4ffd95d2df777f07a77a5f5613853ed6749209181dfa/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f68616e646c65626172732f76657273696f6e2e737667)](https://packagist.org/packages/xp-forge/handlebars)

The [Handlebars template language](http://handlebarsjs.com/) implemented for the XP Framework.

```
use com\handlebarsjs\HandlebarsEngine;

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

Templating
----------

[](#templating)

Templates can be loaded from the file system. The following loads and transforms the template *src/main/handlebars.handlebars*:

```
use com\handlebarsjs\HandlebarsEngine;

$engine= (new HandlebarsEngine())->withTemplates('src/main/handlebars');
$transformed= $engine->transform('hello', [
  'name' => 'World'
]);
```

Templates can also be declared inline:

```
use com\handlebarsjs\HandlebarsEngine;

$engine= (new HandlebarsEngine())->withTemplates(['test' => 'Hello {{name}}']);
$transformed= $engine->transform('hello', [
  'name' => 'World'
]);
```

If you need more flexibility, you can implement and then pass instances of the following:

- `new com.github.mustache.InMemory([:string] $templates))`
- `new com.github.mustache.FilesIn(string|io.Folder $arg, string[] $extensions)`
- `new com.github.mustache.ResourcesIn(string|lang.IClassLoader $arg, string[] $extensions)`
- (Your own implementation of `com.github.mustache.templates.Templates`)

Helpers supported
-----------------

[](#helpers-supported)

The following helpers are built in:

### The "if" block

[](#the-if-block)

```
{{#if licence}}
  A licence is available
{{/if}}

{{#if licence}}
  A licence is available
{{else}}
  Warning: No licence is available!
{{/if}}

{{#if content}}
  Content
{{else if hub}}
  Hub
{{else}}
  Default
{{/if}}
```

### The "unless" block

[](#the-unless-block)

```
{{#unless licence}}
  Warning: No licence is available!
{{/unless}}
```

### The "with" block

[](#the-with-block)

```
{{#with person}}
  Full name: {{firstName}} {{lastName}}
{{/with}}
```

### The "each" block

[](#the-each-block)

```

  {{#each students}}
    Student's name: {{firstName}} {{lastName}}
  {{/each}}

```

All of the above block helpers support the `else` statement.

### The "log" helper

[](#the-log-helper)

```
{{log '"Hello", Frank\'s mother said.'}}
{{log 'No publishers for' category level="warn"}}
```

To enable logging, pass either a closure or a `util.log.LogCategory` instance to the engine:

```
use util\log\Logging;
use util\cmd\Console;

// Use a logger category:
$logger= Logging::named('trace')->toConsole();

// Or a closure:
$logger= function($args, $level) { Console::writeLine('[', $level, '] ', ...$args); };

$engine= (new HandlebarsEngine())->withLogger($logger);
$engine->render(...);
```

Custom helpers
--------------

[](#custom-helpers)

To add custom helpers, use *withHelpers()* and pass functions. The following yields *Hello WORLD*:

```
use com\handlebarsjs\HandlebarsEngine;

$engine= (new HandlebarsEngine())->withHelper('upper', function($node, $context, $options) {
  return strtoupper($options[0]);
});
$transformed= $engine->render('Hello {{upper name}}', [
  'name' => 'World'
]);
```

The parameters passed are the following:

- **node:** The current node, a `com.github.mustache.Node` instance
- **context:** The current context, a `com.github.mustache.Context` instance
- **options:** The resolved options passed, in the above case the string "World" (which is what *name* resolves to)

Futher reading
--------------

[](#futher-reading)

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance60

Regular maintenance activity

Popularity27

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~76 days

Recently: every ~169 days

Total

52

Last Release

254d ago

Major Versions

v5.3.0 → v6.0.02021-05-02

v6.2.0 → v7.0.02021-10-21

v7.1.1 → v8.0.02022-10-08

v8.1.1 → v9.0.02023-07-23

v9.5.0 → v10.0.02025-05-04

PHP version history (5 changes)v0.1.0PHP &gt;=5.4.0

v1.0.0PHP &gt;=5.5.0

v3.0.0PHP &gt;=5.6.0

v5.0.0PHP &gt;=7.0.0

v10.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 (335 commits)")

---

Tags

handlebarsphptemplatesxp-frameworkmodulexp

### Embed Badge

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

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

PHPackages © 2026

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