2017 © Pedro Peláez
 

library bh

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

image

devgroup/bh

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

  • Wednesday, October 26, 2016
  • by bethrezen
  • Repository
  • 6 Watchers
  • 0 Stars
  • 529 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 9 Forks
  • 0 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

bh-php

![Gitter](https://badges.gitter.im/Join Chat.svg) Latest Stable Version Total Downloads, (*1)

Build Status Coverage Status Dependency Status, (*2)

BH is a processor that converts BEMJSON to HTML. Or in other words a template engine., (*3)

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

Table of Contents

Friendly Packages

Installation

Via composer

Execute in your shell:, (*5)

php composer.phar require bem/bh

or (if you have composer in your path), (*6)

composer require bem/bh

And use in your code:, (*7)

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

Manual installation

Download using git (execute this in your shell):, (*8)

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

Using wget and tar:, (*9)

# 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 and unpack to ./vendor/bem/bh path (or any path you want)., (*10)

And use in your code:, (*11)

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

Usage

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

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

To load this file format use include and run technique:, (*13)

// 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:, (*14)

$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., (*15)

Common use case:, (*16)

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' ]);
// '<button class="button"></button>'

Conversion

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

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)., (*18)

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)., (*19)

Syntax:, (*20)

/**
 * 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 or README.ru.md., (*21)

License

The MIT Licence., (*22)

The Versions

26/10 2016

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

26/10 2016

5.2.1

5.2.1.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

28/09 2016

5.2.0

5.2.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

02/06 2016

5.1.1

5.1.1.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

06/05 2016

5.1.0

5.1.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

06/05 2016

5.0.0

5.0.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

25/01 2016

4.4.0

4.4.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

25/01 2016

4.3.0

4.3.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

25/01 2016

4.2.0

4.2.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

18/07 2015

4.1.0

4.1.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

23/04 2015

dev-warm-up

dev-warm-up

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

23/04 2015

4.0.0

4.0.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

22/04 2015

3.3.1

3.3.1.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

21/04 2015

dev-issues/6

dev-issues/6

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

21/04 2015

dev-perf-debug

dev-perf-debug

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

12/02 2015

3.3.0

3.3.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

25/11 2014

3.2.2

3.2.2.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

25/11 2014

dev-feature/tparam-by-ref

dev-feature/tparam-by-ref

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson

25/11 2014

dev-hotfix/optimize-coverage

dev-hotfix/optimize-coverage

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alexej Yaroshevich

bem bh bemjson