bem / bh
模板引擎。BEMJSON -> HTML处理器。https://github.com/bem/bh的PHP端口
4.1.0
2015-07-18 18:41 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/php-invoker: 1.*@stable
- phpunit/phpunit: 4.3.*
This package is not auto-updated.
Last update: 2024-09-14 17:12:54 UTC
README

BH是一个将BEMJSON转换为HTML的处理程序。换句话说,它是一个模板引擎。
支持PHP 5.4+
(由于缺少大量所需功能,不与HHVM
兼容)
目录
友好包
- 带有BH.PHP技术的项目模板
- 带有BH.PHP模板的BEM核心库 - tmp分支
- 带有BH.PHP模板的BEM组件库 - tmp分支
安装
通过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
使用wget
和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
或直接下载最新版本并将其解压到./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.md或README.ru.md中的更多示例。