PHPackages                             cradlephp/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. [Templating &amp; Views](/categories/templating)
4. /
5. cradlephp/handlebars

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

cradlephp/handlebars
====================

Ultra fast PHP7 implementation of handlebars.js

2.3.0(5y ago)259.2k↑11.4%1[1 issues](https://github.com/CradlePHP/Handlebars/issues)1MITPHP

Since Mar 11Pushed 5y ago1 watchersCompare

[ Source](https://github.com/CradlePHP/Handlebars)[ Packagist](https://packagist.org/packages/cradlephp/handlebars)[ Docs](https://cradlephp.github.io/)[ RSS](/packages/cradlephp-handlebars/feed)WikiDiscussions master Synced 1mo ago

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

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

[](#handlebarsphp)

Ultra fast PHP7 implementation of handlebars.js

[![Travis CI](https://camo.githubusercontent.com/0e0b444c4f3a58c8d7b6b356ec6ee5574ac131942d65fe4c83e84024054f3ced/68747470733a2f2f7472617669732d63692e6f72672f437261646c655048502f48616e646c65626172732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/CradlePHP/handlebars)[![Coverage Status](https://camo.githubusercontent.com/6f1df96da105046052bdc602c5a2340b1da1157aa0d04470320ec0658664c58c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f437261646c655048502f48616e646c65626172732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/CradlePHP/handlebars?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/fb15ced1f0765f664746ca9ac5d908af93f62dd376ed154523fe70178035f093/68747470733a2f2f706f7365722e707567782e6f72672f637261646c657068702f68616e646c65626172732f762f737461626c65)](https://packagist.org/packages/cradlephp/handlebars)[![Total Downloads](https://camo.githubusercontent.com/d0734e11ada998788f724c230906afcffa8316753763a48310b364721ca86dab/68747470733a2f2f706f7365722e707567782e6f72672f637261646c657068702f68616e646c65626172732f646f776e6c6f616473)](https://packagist.org/packages/cradlephp/handlebars)[![Latest Unstable Version](https://camo.githubusercontent.com/96f8188c2f64f3103a54f53a5396e728293464ea3e409a762dbb043fa3b5cbbd/68747470733a2f2f706f7365722e707567782e6f72672f637261646c657068702f68616e646c65626172732f762f756e737461626c65)](https://packagist.org/packages/cradlephp/handlebars)[![License](https://camo.githubusercontent.com/5f0223237cf9dde00891dae98d6bf0c4b06a15494a0bf5efdac8917c8fee4b92/68747470733a2f2f706f7365722e707567782e6f72672f637261646c657068702f68616e646c65626172732f6c6963656e7365)](https://packagist.org/packages/cradlephp/handlebars)

Install
-------

[](#install)

```
$ composer install cradlephp/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 Cradle\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 `Eden\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 `Eden\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 `Eden\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 `Eden\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 `Eden\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 `Eden\Handlebars\Index`

#### Example

[](#example-6)

```
$handlebars->unregisterPartial();

```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity70

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

Recently: every ~197 days

Total

11

Last Release

1892d ago

Major Versions

1.0.3 → 2.1.x-dev2018-11-07

2.3.x-dev → 3.0.x-dev2021-03-14

### Community

Maintainers

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

---

Top Contributors

[![cjzamora](https://avatars.githubusercontent.com/u/5561932?v=4)](https://github.com/cjzamora "cjzamora (2 commits)")

---

Tags

cradlecradlephphandlebarstemplate-enginev2v3-prototypehandlebarscradlecradlephp

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[zordius/lightncandy

An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ).

60910.5M45](/packages/zordius-lightncandy)[salesforce/handlebars-php

Handlebars processor for php

80713.2k11](/packages/salesforce-handlebars-php)[proai/laravel-handlebars

A Laravel wrapper for LightnCandy for using the Handlebars (and Mustache) template engine.

38204.7k](/packages/proai-laravel-handlebars)[voodoophp/handlebars

Handlebars processor for php

34158.9k2](/packages/voodoophp-handlebars)[devtheorem/php-handlebars

A blazing fast, spec-compliant PHP implementation of Handlebars.

1924.8k2](/packages/devtheorem-php-handlebars)

PHPackages © 2026

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