PHPackages                             thoom/phandlebars - 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. thoom/phandlebars

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

thoom/phandlebars
=================

Handlebars.js based templating engine for the Silex framework

326JavaScript

Since Feb 2Pushed 13y ago1 watchersCompare

[ Source](https://github.com/thoom/phandlebars)[ Packagist](https://packagist.org/packages/thoom/phandlebars)[ RSS](/packages/thoom-phandlebars/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Thoom Phandlebars
=================

[](#thoom-phandlebars)

Summary
-------

[](#summary)

Phandlebars allows you to use Handlebars templates in your Silex applications. Rather than reimplementing the Handlebars library in PHP, this provider uses server-side Javascript (primarily with node.js) to both compile the Handlebars templates you've created (making the generated JS file also available for your client-side handling) and process templates on the server.

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

[](#installation)

There are two parts to the installation: the PHP hooks into your Silex application and the required server-side applications.

### Silex Application

[](#silex-application)

There are these options currently:

1. **debug**: If debug is true, then the compiled file will be overwritten with each request. If false, it will only build the compiled file if it doesn't exist.
2. **library**: By default, the compiled JS file will use the Handlebars runtime library. If you need to use the full library instead (for instance, if you dynamically compile on the client side), then pass in `HandlebarsServiceProvider::LIBRARY_FULL`.
3. **minify**: If true, the script will attempt to minify the file using the `runtime.minify` command.
4. **path.compiled.client**: This is the full file path of where you want the compiled client-only Javascript file to reside. The location should be writable by your web user.
5. **path.compiled.server**: This is the full file path of where you want the server compiled Javascript file to reside.
6. **path.templates.client**: The directory where all of the client-only Handlebars templates are stored. Note that all of your templates should end with *.handlebars*.
7. **path.templates.server**: The directory where all of the server-only Handlebars templates are stored. Note that all of your templates should end with *.handlebars*.
8. **runtime.minify**: If not set, the script will attempt to minify the client file using `uglifyjs` command.
9. **runtime.node**: If not set, the script will attempt to run node from using `node` command.

As an example:

```
$app->register(new Thoom\Provider\HandlebarsServiceProvider(), array(
    'handlebars.options' => array(
        'debug' => $app['debug'],
        'minify' => true,
        'path.compiled.client' => $app['cache_dir'] . '/js/handlebars-compiled.js',
        'path.compiled.server' => $app['cache_dir'] . '/compiled.js',
        'path.templates.client' => __DIR__ . '/templates/client',
        'path.templates.server' => __DIR__ . '/templates/server',
    )
));

```

### Server

[](#server)

The provider needs a few server cli applications installed in order to render and compile templates.

#### Node.js

[](#nodejs)

Easy to install using the package manager of your choice. For instance, with Ubuntu:

```
$ apt-get install nodejs build-essential

```

This assumes that your node executable is installed and accessible from `node`. If you need to use a different command, change the `runtime.node` configuration option.

#### Handlebars.js

[](#handlebarsjs)

You'll need to install using NPM.

```
$ apt-get install npm
$ npm install -g handlebars

```

#### UglifyJS (optional)

[](#uglifyjs-optional)

If you want to minify your compiled templates, you'll need to install UglifyJS.

```
$ npm install -g uglify-js

```

If you prefer another minifier, note that it just needs to accept data from `STDIN` and outputs to `STDOUT`. To change the minifier, change the `runtime.minify` configuration option.

To Use
------

[](#to-use)

### Silex Application

[](#silex-application-1)

#### Precompile

[](#precompile)

You may wish to precompile the script. This can be accomplished by calling the renderer from your application using the following command:

```
$app['handlebars']->server();

```

To precompile handlebars for client only scripts:

```
$app['handlebars']->client();

```

#### Server-side rendering

[](#server-side-rendering)

To render a server-side template for "index.handlebars":

```
$app->get('/', function (Application $app) {
    return $app['handlebars']->render('index');
})->bind('homepage');

```

To add a global variable (like a user array) to be available to your templates:

```
$app['handlebars']->addGlobal('user', $userArray);

```

To pass data to the template at render time, pass in a array as the second argument:

```
$app['handlebars']->render('index', array('foo' => 'fez'));

```

To return an Http header other than 200, pass in a 3rd argument:

```
$app['handlebars']->render('not-found', array('foo' => 'fez'), 404);

```

**Note:** *Since the Handlebars templates are processed in Javascript using node, you can only pass items that are JSON serializable.*

### Handlebars templates

[](#handlebars-templates)

In addition to the standard [Handlebars.js options](https://handlebarsjs.com), you have a few more options that allow you to extend templates similar to Twig.

To create a master template ('master.handlebars'), use the {{#block}} tag:

```

        {{#block "title"}}Master Template{{/block}}

    ...

```

In your child template, you would use the {{#override}} and {{extend}} tags:

```
{{#override "title"}}Child Template{{/override}}

{{extend "master"}}

```

This would print:

```

        Child Template

    ...

```

Notice that unlike Twig templates, the extend tag is found at the bottom of the script. This is required because of how Handlebars parses templates.

#### Additional Tags

[](#additional-tags)

##### path

[](#path)

This convenience expression takes all of your Silex named routes and makes them available in your templates.

If your named route "login" has a path of "/login", you'd use:

```
{{path "login" }}

```

If your named route "section" has a path of "/section/{section}", you'd use:

```
{{path "section" section="my-section" }}

```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/3237c22aa7a2c0bd68c270764cd30cf9b4b3a4351b26c0fd252e6ccd55d72994?d=identicon)[thoom](/maintainers/thoom)

### Embed Badge

![Health badge](/badges/thoom-phandlebars/health.svg)

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

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

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