bem/bh

模板引擎。BEMJSON -> HTML处理器。https://github.com/bem/bh的PHP端口

4.1.0 2015-07-18 18:41 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:12:54 UTC


README

![Gitter](https://badges.gitter.im/Join Chat.svg) 最新稳定版本 总下载量

Build Status Coverage Status Dependency Status

BH是一个将BEMJSON转换为HTML的处理程序。换句话说,它是一个模板引擎。

支持PHP 5.4+(由于缺少大量所需功能,不与HHVM兼容)

目录

友好包

安装

通过composer

在您的shell中执行

php composer.phar require bem/bh

或者(如果您路径中有composer

composer require bem/bh

然后在您的代码中使用

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

手动安装

使用git下载(在您的shell中执行此操作)

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

使用wgettar

# 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

或直接下载最新版本并将其解压到./vendor/bem/bh路径(或任何您想要的路径)。

然后在您的代码中使用

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

使用方法

项目中的BH文件具有.bh.php后缀(例如,page.bh.php)。该文件以类似CommonJS的格式形成

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

要加载此文件格式,请使用include和run技术

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

// ...

这允许您暂时拥有多个实例

$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);

使用apply方法将BEMJSON的源树转换为输出HTML。使用processBemJson方法以详细BEMJSON树形式获取中间结果。

常用案例

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

转换

用于BEMJSON的工作函数是模板。使用match方法声明模板。BEMJSON转换的逻辑在函数体中声明。

模板函数提供了两个参数

  • $ctx - \BEM\Context类的实例;
  • $json - \BEM\Json类的实例(当前的BEMJSON树节点)。

注意:不要直接修改$json对象。请使用$ctx对象的方法。我们建议您仅将$json对象用于读取(另请参阅$ctx->json()方法)。

语法

/**
 * 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
]);

请参阅README.mdREADME.ru.md中的更多示例。

许可证

MIT许可证.