Template engine. BEMJSON -> HTML processor. PHP port of

4.1.0 2015-07-18 18:41 UTC

This package is not auto-updated.

Last update: 2023-03-18 09:04:54 UTC


![Gitter]( Chat.svg) Latest Stable Version Total Downloads

Build Status Coverage Status Dependency Status

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

Friendly Packages


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

Download using git (execute this in your shell):

# via git
git clone ./vendor/bem/bh

Using wget and tar:

# via wget + tar
wget # 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 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();
// ...


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

return function ($bh) {
    // ...

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('');
$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/');
$mergedMatchers = include('bundles/merged/');

// 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

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) {

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

$bh->apply([ 'block' => 'button' ]);
// '<button class="button"></button>'


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).


 * 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 or


The MIT Licence.