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

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

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

Handlebars templates for XP web frontends

v4.3.0(7mo ago)114.0k—0%[1 PRs](https://github.com/xp-forge/handlebars-templates/pulls)BSD-3-ClausePHPPHP &gt;=7.4.0CI passing

Since Feb 13Pushed 7mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (6)Versions (36)Used By (0)

Handlebars for XP web frontends
===============================

[](#handlebars-for-xp-web-frontends)

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

Handlebars template engine implementation to be used in conjunction with [XP web frontends](https://github.com/xp-forge/frontend).

Example
-------

[](#example)

Wiring happens inside your web application:

```
use web\frontend\{Frontend, AssetsFrom, HandlersIn, Handlebars};
use web\Application;

class App extends Application {

  /** Returns routing for this web application */
  public function routes() {
    return [
      '/static' => new AssetsFrom($this->environment->path('src/main/webapp')),
      '/'       => new Frontend(
        new HandlersIn('com.example.app.web'),
        new Handlebars($this->environment->path('src/main/handlebars'))
      )
    ];
  }
}
```

Templates
---------

[](#templates)

The templates live in `src/main/handlebars`, their names corresponding to lowercased version of the handlers' names (`Home::class` =&gt; `home.handlebars`).

Templates support YAML front matter, which can be used to set defaults for template globals. Example:

```
---
nav:
  /: Home
  /about: About
  /login: Login
---
DOCTYPE html>

  ...

      {{#each nav}}
        {{.}}
      {{/each}}

```

Fragments
---------

[](#fragments)

Instead of rendering an entire template, we can render special inline partials we call *fragments*. They are declared as follows:

```
DOCTYPE html>

  ...

    {{#*fragment "listing"}}

        {{#each items}}
          {{.}}
        {{/each}}

    {{/fragment}}

```

...and are rendered by selecting them via *fragment()* in our handler:

```
use web\frontend\{Handler, Get};

#[Handler]
class Index {
  private $list= ['One', 'Two', 'Three'];

  #[Get]
  public function index() {
    return View::named('index')->with(['list' => $this->list]);
  }

  #[Get('/listing')]
  public function partial() {
    return View::named('index')->fragment('listing')->with(['list' => $this->list]);
  }
}
```

Accessing the URI */listing* will render only the `...` instead of the entire document. These fragments can be used in conjunction with frameworks like [htmx](https://htmx.org/), see [this gist for an example](https://gist.github.com/thekid/019b12627f129afe7bdaea4cc594b29a).

Helpers
-------

[](#helpers)

On top of the [built-in functionality in Handlebars](https://github.com/xp-forge/handlebars), this library includes the following essential helpers:

- `encode`: Performs URL-encoding
- `json`: Performs JSON encoding, pretty-printing when given `format=true`.
- `equals`: Tests arguments for equality
- `contains`: Tests whether a string or array contains a certain value
- `size`: Returns string length or array size
- `min`: Returns smallest element
- `max`: Returns largest element
- `any`: Test whether any of the given arguments is truthy
- `none`: Test whether none of the given arguments is truthy
- `all`: Test whether all of the given arguments is truthy

### Date handling

[](#date-handling)

```
use util\TimeZone;
use web\frontend\Handlebars;
use web\frontend\helpers\Dates;

new Handlebars($templates, [new Dates()]);

// Pass timezone or NULL to use local timezone
new Handlebars($templates, [new Dates(new TimeZone('Europe/Berlin'))]);
new Handlebars($templates, [new Dates(null)]);

// Pass default and named date format
new Handlebars($templates, [new Dates(null, [null => 'd.m.Y'])]);
new Handlebars($templates, [new Dates(null, ['us:short' => 'Y-m-d'])]);
```

The `date` helper accepts anything the `util.Date` class accepts as constructor argument, or a `util.Date` instance itself. To format the date, the `format` argument can be used with anything the `util.Date::toString()` method accepts. Here are some examples:

```
{{date "2021-02-13"}}
{{date "13.02.2021 17:56:00"}}
{{date 1613209181}}
{{date 1613209181279 timestamp="ms"}}
{{date created}}
{{date created format="d.m.Y"}}
{{date created format="us:short"}}
{{date created timezone="America/New_York"}}
```

### Logging

[](#logging)

The `log` helper will echo the arguments passed to it:

```
{{log user}}
{{log "User profile:" user}}
```

When using the development webserver, this shows the debug page:

[![Debug page](https://user-images.githubusercontent.com/696742/107873960-89cdc800-6eb6-11eb-954b-8b00324cce74.png)](https://user-images.githubusercontent.com/696742/107873960-89cdc800-6eb6-11eb-954b-8b00324cce74.png)

In production environments, logs will end up on the server's standard output:

[![Console output](https://user-images.githubusercontent.com/696742/107874105-838c1b80-6eb7-11eb-8c7e-ee257ef1d92d.png)](https://user-images.githubusercontent.com/696742/107874105-838c1b80-6eb7-11eb-8c7e-ee257ef1d92d.png)

Standalone
----------

[](#standalone)

To use the template engine by itself, simply instantiate and call its *render()* method:

```
use web\frontend\Handlebars;

$engine= new Handlebars('.');
echo $engine->render('home', []);
```

This will render the *home.handlebars* file and return the result as a string.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance62

Regular maintenance activity

Popularity26

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

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 ~52 days

Recently: every ~133 days

Total

33

Last Release

233d ago

Major Versions

v0.9.0 → v1.0.02021-07-17

v1.2.0 → v2.0.02022-10-08

v2.5.0 → v3.0.02023-07-29

v3.5.1 → v4.0.02025-05-04

PHP version history (2 changes)v0.1.0PHP &gt;=7.0.0

v4.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 (141 commits)")

---

Tags

assetshandlebars-helpershandlebars-partialshandlebars-templatesphp7php8yaml-frontmattermodulexp

### Embed Badge

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

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

###  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)
