PHPackages                             bem/bh - 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. bem/bh

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

bem/bh
======

Template engine. BEMJSON -&gt; HTML processor. PHP port of https://github.com/bem/bh

4.1.0(10y ago)347548[10 issues](https://github.com/bem/bh-php/issues)MITPHPPHP &gt;=5.4.0

Since Nov 25Pushed 5y ago27 watchersCompare

[ Source](https://github.com/bem/bh-php)[ Packagist](https://packagist.org/packages/bem/bh)[ RSS](/packages/bem-bh/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (8)Used By (0)

bh-php
======

[](#bh-php)

[!\[Gitter\](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/bem/talk?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)[![Latest Stable Version](https://camo.githubusercontent.com/fbff7eb8f32f665952b97794f6634ae13376cc7375b475b6b4bb60f5854c58c2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62656d2f62682e7376673f7374796c653d666c6174)](https://packagist.org/packages/bem/bh)[![Total Downloads](https://camo.githubusercontent.com/79d8e0aa904f8dfc603433f9cbea69d427e90209a8bf044ccd2febc4794ecf5c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f62656d2f62682e7376673f7374796c653d666c6174)](https://packagist.org/packages/bem/bh)

[![Build Status](https://camo.githubusercontent.com/9dade000ff3fd02c51591c79d5ae0177bd5d7d7b21ba0714573d7725bd9490b9/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f62656d2f62682d7068702e7376673f7374796c653d666c6174)](https://travis-ci.org/bem/bh-php)[![Coverage Status](https://camo.githubusercontent.com/fe983218eef3bf5e6cbf9ecabf8315c4b6af178e6c91bf03ef0863d13e88e4b9/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f62656d2f62682d7068702e7376673f7374796c653d666c6174)](https://coveralls.io/r/bem/bh-php)[![Dependency Status](https://camo.githubusercontent.com/c543513070d8421dcf61a077e207990e2361e2e38806708a05d79afb1fa6eb9f/68747470733a2f2f696d672e736869656c64732e696f2f67656d6e617369756d2f62656d2f62682d7068702e7376673f7374796c653d666c6174)](https://gemnasium.com/bem/bh-php)

BH is a processor that converts BEMJSON to HTML. Or in other words a template engine.

Works with `PHP 5.4+` (doesn't work with `HHVM` 'cause it lacks a lot of required functionality)

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Friendly Packages](#friendly-packages)
- [Usage](#usage)
- [Conversion](#conversion)

Friendly Packages
-----------------

[](#friendly-packages)

- [Project Stub with BH.PHP tech](https://github.com/bem/project-stub/tree/php-bem-bh)
- [BEM Core Library with BH.PHP templates](https://github.com/zxqfox/bem-core/tree/feature/php-bh-templates%40v2) - tmp branch
- [BEM Components Library with BH.PHP templates](https://github.com/zxqfox/bem-components/tree/feature/php-bh-templates%40v2) - tmp branch

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

[](#installation)

### Via composer

[](#via-composer)

Execute in your shell:

```
php composer.phar require bem/bh

```

or (if you have `composer` in your path)

```
composer require bem/bh

```

And use in your code:

```
require "vendor/autoload.php";
$bh = new \BEM\BH();
// ...
```

### Manual installation

[](#manual-installation)

Download using `git` (execute this in your shell):

```
# via git
git clone https://github.com/bem/bh-php.git ./vendor/bem/bh

```

Using `wget` and `tar`:

```
# via wget + tar
wget https://github.com/bem/bh-php/archive/master.tar.gz # download archive
tar -xzvf master.tar.gz --exclude=tests        # extract
[ ! -d ./vendor/bem ] && mkdir ./vendor/bem -p # create vendor director
mv ./bh-php-master ./vendor/bem/bh             # move library to vendor
rm master.tar.gz                               # cleanup

```

Or just download [the latest version](https://github.com/bem/bh-php/archive/master.zip) and unpack to `./vendor/bem/bh` path (or any path you want).

And use in your code:

```
// manual installation
require "vendor/bem/bh/index.php";
$bh = new \BEM\BH();
// ...
```

Usage
-----

[](#usage)

BH files within a project have `.bh.php` suffix (for example, `page.bh.php`). The file is formed in CommonJS-like format:

```
return function ($bh) {
    $bh->match(/*...*/);
    // ...
};
```

To load this file format use include and run technique:

```
// Instantiate BH object
$bh = new \BEM\BH();

// Load and apply matchers to BH object in $bh
$fn = include('file.bh.php');
$fn($bh); // done. and nothing in global

// ...
```

This allows you to have several instances at the moment:

```
$bh1 = new \BEM\BH();
$bh2 = new \BEM\BH();

// load matchers
$indexMatchers = include('bundles/index/index.bh.php');
$mergedMatchers = include('bundles/merged/merged.bh.php');

// apply them
$indexMatchers($bh1); // bh1 now contains matchers for index page only
$mergedMatchers($bh2); // bh2 now contains all matchers

// use it with the same bemjson data
$bh1->apply($bemjson);
$bh2->apply($bemjson);
```

Use `apply` method to convert source tree of BEMJSON into an output HTML. Use `processBemJson` method to get an interim result in detailed BEMJSON tree form.

Common use case:

```
require "vendor/autoload.php";
$bh = new \BEM\BH();
$bh->match('button', function ($ctx) {
    $ctx->tag('button');
});

$bh->processBemJson([ 'block' => 'block' ]);
// [ 'block' => 'button', 'mods' => new Mods(), 'tag' => 'button' ]

$bh->apply([ 'block' => 'button' ]);
// ''
```

Conversion
----------

[](#conversion)

Working functions for BEMJSON are **templates**. Use `match` method to declare templates. Logic of BEMJSON conversion is declared in a function body.

There are two arguments provided to a template function:

- `$ctx` – instance of `\BEM\Context` class;
- `$json` – instance of `\BEM\Json` class (current BEMJSON tree node).

*NB*: Do not make changes directly in `$json` object. Use methods of `$ctx` object instead. We recommend you to use `$json` object for reading only (see also `$ctx->json()` method).

Syntax:

```
/**
 * Register matchers
 * @param string|array $expression bem css expression
 * @param closure [$matcher]
 * @return \BEM\BH
 */
$bh->match(/*string*/ $expression, function (\BEM\Context $ctx, \BEM\Json $json) {
    // ... actions
});

// or...
$bh->match([/*string*/ $expression], function (\BEM\Context $ctx, \BEM\Json $json) {
    // ... actions
});

// or...
$bh->match(/*array*/ $matchers = [
    "$expression" => function(\BEM\Context $ctx, \BEM\Json $json) {
        // ... actions
    },
    // ... more matchers
]);
```

Look at more examples in [README.md](https://github.com/bem/bh/blob/master/README.md) or [README.ru.md](https://github.com/bem/bh/blob/master/README.ru.md).

License
-------

[](#license)

[The MIT Licence](LICENSE).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance14

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 78.6% 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 ~59 days

Total

5

Last Release

3952d ago

Major Versions

v3.3.1 → 4.0.02015-04-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/273c3a9702495ba0ebc3e3ec1a3008e9414c256a96e452c37e605c235fb55363?d=identicon)[zxqfox](/maintainers/zxqfox)

---

Top Contributors

[![qfox](https://avatars.githubusercontent.com/u/677518?v=4)](https://github.com/qfox "qfox (11 commits)")[![bethrezen](https://avatars.githubusercontent.com/u/260284?v=4)](https://github.com/bethrezen "bethrezen (2 commits)")[![gitter-badger](https://avatars.githubusercontent.com/u/8518239?v=4)](https://github.com/gitter-badger "gitter-badger (1 commits)")

---

Tags

bembemjsonphpphp-templatetemplate-engineBEMbhbemjson

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bem-bh/health.svg)

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

###  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)[symfony/ux-icons

Renders local and remote SVG icons in your Twig templates.

545.8M69](/packages/symfony-ux-icons)

PHPackages © 2026

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