PHPackages                             phpugph/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. phpugph/handlebars

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phpugph/handlebars
==================

Ultra fast PHP8 implementation of handlebars.js

1.0.6(2y ago)32.3k11MITPHP

Since Mar 28Pushed 2y agoCompare

[ Source](https://github.com/phpugph/handlebars)[ Packagist](https://packagist.org/packages/phpugph/handlebars)[ RSS](/packages/phpugph-handlebars/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (4)Versions (9)Used By (1)

handlebars.php
==============

[](#handlebarsphp)

Ultra fast PHP8 implementation of handlebars.js

[![Build Status](https://camo.githubusercontent.com/f5528d5dd317c2ed72d0508bd3e74f0135e04a80b4cd834914363936175bfbbd/68747470733a2f2f7472617669732d63692e636f6d2f706870756770682f68616e646c65626172732e7376673f6272616e63683d6d61696e)](https://travis-ci.com/phpugph/handlebars)[![Coverage Status](https://camo.githubusercontent.com/cd231af2f4051be1f1ef7b0ef5187ff07e82496e9de557cf8b12508768867995/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f706870756770682f68616e646c65626172732f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/phpugph/handlebars?branch=main)[![Latest Stable Version](https://camo.githubusercontent.com/068658537592bff20e79c947145ae7a0798e6d6328e11e6499e3069c8a292ac3/68747470733a2f2f706f7365722e707567782e6f72672f706870756770682f68616e646c65626172732f762f737461626c65)](https://packagist.org/packages/phpugph/handlebars)[![Total Downloads](https://camo.githubusercontent.com/38a7310728ee83c3d7ca8e89743c4ce168f72d01588130e511a9ed5a1e47b04a/68747470733a2f2f706f7365722e707567782e6f72672f706870756770682f68616e646c65626172732f646f776e6c6f616473)](https://packagist.org/packages/phpugph/handlebars)[![Latest Unstable Version](https://camo.githubusercontent.com/966a8f448c08fcbff46f4f7bb25fc82f1dbf7ea916359c7129ad1ee92ee45de8/68747470733a2f2f706f7365722e707567782e6f72672f706870756770682f68616e646c65626172732f762f756e737461626c65)](https://packagist.org/packages/phpugph/handlebars)[![License](https://camo.githubusercontent.com/1c8d7b8cb78eb318533e0ec5b751384fdeb7c68a1e8b2c7435d006a25e8b6ccc/68747470733a2f2f706f7365722e707567782e6f72672f706870756770682f68616e646c65626172732f6c6963656e7365)](https://packagist.org/packages/phpugph/handlebars)

Install
-------

[](#install)

```
$ composer install phpugph/handlebars
```

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

[](#introduction)

PHP Handlebars and JS interface to match with compile time helper support and super nice compile time error reporting. This version of Handlebars is based on caching the compiled templates and inherently made the overall compile times faster. Loading at ~50ms uncached and ~30ms cached.

Basic Usage
-----------

[](#basic-usage)

#### Rendering

[](#rendering)

```
use Handlebars\HandlebarsHandler as Handlebars;
$handlebars = new Handlebars();
$template = $handlebars->compile('{{foo}} {{bar}}');

echo $template(['foo' => 'BAR', 'bar' => 'ZOO']);
```

#### Registering Helpers

[](#registering-helpers)

```
$handlebars->registerHelper('bar', function($options) {
    return 'ZOO';
});

$template = $handlebars->compile('{{foo}} {{bar}}');

echo $template(['foo' => 'BAR']);

```

#### Registering Partials

[](#registering-partials)

```
$handlebars->registerPartial('bar', 'zoo');
$template = $handlebars->compile('{{foo}} {{> bar}}');

echo $template(['foo' => 'BAR']);

```

---

Features
--------

[](#features)

- PHP API - designed to match the handlebars.js documentation
    - registerHelper() - Matches exactly what you expect from handlebars.js (except it's PHP syntax)
    - registerPartial() - accepts strings and functions as callbacks
    - Literals like `{{./foo}}` and `{{../bar}}` are evaluated properly
    - Comments like `{{!-- Something --}}` and `{{! Something }}` supported
    - Trims like `{{~#each}}` and `{{~foo~}}` supported
    - Mustache backwards compatibility `{{#foo}}{{this}}{{/foo}}`
    - Tokenizer helpers to optimize custom code generation to cache
    - Event handlers for unknown helpers and unknown partials
- Default Helpers matching handlebars.js
    - each - and `{{#each foo as |value, key|}}`
        - Please note that there is an issue with `each` being slow depending on the size of the object
        - We need help optimizing this
    - with
    - unless
    - if

De-Features (or whatever the opposite of features is)
-----------------------------------------------------

[](#de-features-or-whatever-the-opposite-of-features-is)

- Does not support file templates.
    - You need to load them up and pass it into Handlebars.
    - If this is a problem you should consider other Handlebars PHP libraries
    - You can always create a helper for this
    - This de-feature will be considered upon requests ( create an issue :) )
- Partial Failover
    - Something we haven't had a chance to come around doing yet as we did not have a need
    - This de-feature will be considered upon requests ( create an issue :) )
- Safe String/Escaping
    - PHP has functions that can turn a string "safe".
    - We didn't want to create something that already exists in other contexts
    - This de-feature will be considered upon requests ( create an issue :) )
- Utils
    - PHP has functions that support most of the listed Utils in handlebars.js
    - We didn't want to create something that already exists in other contexts
    - This de-feature will be considered upon requests ( create an issue :) )
- Dynamic Partials
    - At the bottom of our pipe
    - because of it's difficulty to recreate
    - and practicality
    - This de-feature will be considered upon requests ( create an issue :( )
- Inline Partials
    - TODO
- Decorators
    - TODO
- Frames
    - TODO

---

Production Ready
----------------

[](#production-ready)

When your templates are ready for a production (live) environment, it is recommended that caching be used. To enable cache:

- Create a cache folder and make sure permissions are properly set for handlebars to write files to it.
- Enable cache by using `$handlebars->setCache(__DIR__.'/your/cache/folder/location');`
- If the folder location does not exist, caching will be disabled.

---

API
---

[](#api)

### compile

[](#compile)

Returns a callback that binds the data with the template

#### Usage

[](#usage)

```
$handlebars->compile(string $string);

```

#### Parameters

[](#parameters)

- `string $string` - the template string

Returns `function` - the template binding handler

#### Example

[](#example)

```
$handlebars->compile();

```

---

### getCache

[](#getcache)

Returns the active cache path

#### Usage

[](#usage-1)

```
$handlebars->getCache();

```

Returns `Closure`

---

### getHelper

[](#gethelper)

Returns a helper given the name

#### Usage

[](#usage-2)

```
$handlebars->getHelper('if');

```

#### Parameters

[](#parameters-1)

- `string $name` - the name of the helper

Returns `Closure`

---

### getHelpers

[](#gethelpers)

Returns all the registered helpers

#### Usage

[](#usage-3)

```
$handlebars->getHelpers();

```

#### Parameters

[](#parameters-2)

Returns `array`

---

### getPartial

[](#getpartial)

Returns a partial given the name

#### Usage

[](#usage-4)

```
$handlebars->getPartial('foobar');

```

#### Parameters

[](#parameters-3)

- `string $name` - the name of the partial

Returns `string`

---

### getPartials

[](#getpartials)

Returns all the registered partials

#### Usage

[](#usage-5)

```
$handlebars->getPartials();

```

#### Parameters

[](#parameters-4)

Returns `array`

---

### registerHelper

[](#registerhelper)

The famous register helper matching the Handlebars API

#### Usage

[](#usage-6)

```
$handlebars->registerHelper(string $name, function $helper);

```

#### Parameters

[](#parameters-5)

- `string $name` - the name of the helper
- `function $helper` - the helper handler

Returns `Handlebrs\Index`

#### Example

[](#example-1)

```
$handlebars->registerHelper();

```

---

### registerPartial

[](#registerpartial)

Delays registering partials to the engine because there is no add partial method...

#### Usage

[](#usage-7)

```
$handlebars->registerPartial(string $name, string $partial);

```

#### Parameters

[](#parameters-6)

- `string $name` - the name of the helper
- `string $partial` - the helper handler

Returns `Handlebrs\Index`

#### Example

[](#example-2)

```
$handlebars->registerPartial();

```

---

### setCache

[](#setcache)

Enables the cache option

#### Usage

[](#usage-8)

```
$handlebars->setCache(string $path);

```

#### Parameters

[](#parameters-7)

- `string $path` - The cache path

Returns `Handlebrs\Index`

#### Example

[](#example-3)

```
$handlebars->setCache('/path/to/cache/folder');

```

---

### setPrefix

[](#setprefix)

Sets the file name prefix for caching

#### Usage

[](#usage-9)

```
$handlebars->setPrefix(string $prefix);

```

#### Parameters

[](#parameters-8)

- `string $prefix` - Custom prefix name

Returns `Handlebrs\Index`

#### Example

[](#example-4)

```
$handlebars->setPrefix('special-template-');

```

---

### unregisterHelper

[](#unregisterhelper)

The opposite of registerHelper

#### Usage

[](#usage-10)

```
$handlebars->unregisterHelper(string $name);

```

#### Parameters

[](#parameters-9)

- `string $name` - the helper name

Returns `Handlebars\Index`

#### Example

[](#example-5)

```
$handlebars->unregisterHelper();

```

---

### unregisterPartial

[](#unregisterpartial)

The opposite of registerPartial

#### Usage

[](#usage-11)

```
$handlebars->unregisterPartial(string $name);

```

#### Parameters

[](#parameters-10)

- `string $name` - the partial name

Returns `Handlebars\Index`

#### Example

[](#example-6)

```
$handlebars->unregisterPartial();

```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

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

Recently: every ~111 days

Total

8

Last Release

981d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/120378?v=4)[Christian Blanquera](/maintainers/cblanquera)[@cblanquera](https://github.com/cblanquera)

---

Top Contributors

[![cblanquera](https://avatars.githubusercontent.com/u/120378?v=4)](https://github.com/cblanquera "cblanquera (11 commits)")

---

Tags

handlebars

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

PHPackages © 2026

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