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.

v1.1.0(1mo ago)1924.8k—0%62MITPHPPHP &gt;=8.2CI passing

Since Mar 18Pushed 1mo 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 1mo ago

READMEChangelog (10)Dependencies (9)Versions (17)Used By (2)

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

[](#php-handlebars)

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

Originally based on [LightnCandy](https://github.com/zordius/lightncandy), but rewritten to enable full Handlebars.js compatibility without excessive feature flags or performance tradeoffs.

PHP Handlebars compiles and executes complex templates up to 40% faster than LightnCandy:

LibraryCompile timeRuntimeTotal timePeak memory usageLightnCandy 1.2.65.2 ms2.8 ms8.0 ms5.3 MBPHP Handlebars 1.13.5 ms1.6 ms5.1 ms3.6 MB*Tested on PHP 8.5 with the JIT enabled. See the `benchmark` branch to run the same test.*

Features
--------

[](#features)

- Supports all Handlebars syntax and language features, including expressions, subexpressions, helpers, partials, hooks, `@data` variables, whitespace control, and `.length` on arrays.
- 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 full [Handlebars.js spec](https://github.com/jbboehr/handlebars-spec).

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

[](#installation)

```
composer require devtheorem/php-handlebars

```

Usage
-----

[](#usage)

```
use DevTheorem\Handlebars\Handlebars;

$template = Handlebars::compile('Hello {{name}}!');

echo $template(['name' => 'World']); // Hello World!
```

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

[](#precompilation)

Templates can be pre-compiled to native PHP for later execution:

```
use DevTheorem\Handlebars\Handlebars;

$code = Handlebars::precompile('{{org.name}}');

// save the compiled code into a PHP file
file_put_contents('render.php', "
