PHPackages                             devtheorem/php-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. devtheorem/php-handlebars

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

devtheorem/php-handlebars
=========================

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

v2.1.0(3w ago)4048.6k↑12.2%74MITPHPPHP &gt;=8.2CI passing

Since Mar 18Pushed 3w ago4 watchersCompare

[ Source](https://github.com/devtheorem/php-handlebars)[ Packagist](https://packagist.org/packages/devtheorem/php-handlebars)[ RSS](/packages/devtheorem-php-handlebars/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (19)Versions (26)Used By (4)

PHP Handlebars
==============

[](#php-handlebars)

A blazing fast, spec-compliant PHP implementation of [Handlebars](https://handlebarsjs.com).

The syntax of Handlebars is generally a superset of Mustache, so in most cases it is possible to swap out Mustache for Handlebars and continue using the same templates.

Features
--------

[](#features)

- Supports all Handlebars syntax and language features, including expressions, subexpressions, helpers, partials, hooks, `@data` variables, whitespace control, and `.length` on arrays and strings.
- Arrays and objects can both be used as context values. `{{#each}}` over an object iterates its public properties.
- Templates are parsed using [PHP Handlebars Parser](https://github.com/devtheorem/php-handlebars-parser), which implements the same lexical analysis and AST grammar specification as Handlebars.js.
- Tested against the [Handlebars.js spec](https://github.com/jbboehr/handlebars-spec)and the [Mustache spec](https://github.com/mustache/spec).

Performance
-----------

[](#performance)

PHP Handlebars started as a fork of [LightnCandy](https://github.com/zordius/lightncandy), but has been rewritten with an AST-based parser and optimized runtime to enable full Handlebars.js compatibility with better performance.

PHP Handlebars compiles and executes complex templates over 40% faster than LightnCandy, with 60% lower memory usage:

LibraryCompile timeRuntimeTotal timePeak memory usageLightnCandy 1.2.65.0 ms2.4 ms7.4 ms5.3 MBPHP Handlebars 2.12.8 ms1.4 ms4.2 ms1.8 MB*Tested on PHP 8.5 with the JIT enabled. See the `benchmark` branch to run the same test.*

Installation
------------

[](#installation)

```
composer require devtheorem/php-handlebars

```

Usage
-----

[](#usage)

```
use DevTheorem\Handlebars\Handlebars;

$source =  ['name' => 'Jane'],
    'notifications' => [
        ['count' => 4, 'message' => 'new comments', 'time' => '5 min ago'],
        ['count' => 3, 'message' => 'new followers', 'time' => '1 hr ago'],
    ],
];

$template = Handlebars::compile($source);
echo $template($data);
```

Output:

```
Hi Jane, you have 2 new notification(s):

    4 new comments (5 min ago)
    3 new followers (1 hr ago)

```

Precompilation
--------------

[](#precompilation)

Templates and partials can be precompiled to native PHP for later execution, avoiding the overhead of parsing and compilation on each request.

**Build step** - compile all templates in a directory and cache the generated PHP:

```
use DevTheorem\Handlebars\Handlebars;

$templateDir = 'templates';
$cacheDir = 'templateCache';

foreach (glob("$templateDir/*.hbs") ?: [] as $file) {
    $name = basename($file, '.hbs');
    $code = Handlebars::precompile(file_get_contents($file));
    file_put_contents("$cacheDir/$name.php", "
