{"id":18264871,"url":"https://github.com/bem/bh-php","last_synced_at":"2025-04-04T21:30:56.999Z","repository":{"id":23179294,"uuid":"26535395","full_name":"bem/bh-php","owner":"bem","description":"PHP port of https://github.com/bem/bh. It's cool thing but better use this:","archived":false,"fork":false,"pushed_at":"2020-07-24T01:41:26.000Z","size":138,"stargazers_count":34,"open_issues_count":10,"forks_count":9,"subscribers_count":29,"default_branch":"master","last_synced_at":"2025-03-20T19:17:40.410Z","etag":null,"topics":["bem","bemjson","php","php-template","template-engine"],"latest_commit_sha":null,"homepage":"https://github.com/zxqfox/php-fpm-bem-demo-heroku","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-12T12:42:04.000Z","updated_at":"2024-10-04T00:55:24.000Z","dependencies_parsed_at":"2022-08-21T19:50:31.998Z","dependency_job_id":null,"html_url":"https://github.com/bem/bh-php","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bem%2Fbh-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bem%2Fbh-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bem%2Fbh-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bem%2Fbh-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bem","download_url":"https://codeload.github.com/bem/bh-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247252045,"owners_count":20908614,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bem","bemjson","php","php-template","template-engine"],"created_at":"2024-11-05T11:16:21.450Z","updated_at":"2025-04-04T21:30:56.709Z","avatar_url":"https://github.com/bem.png","language":"PHP","readme":"# bh-php\n\n[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/bem/talk?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n [![Latest Stable Version](https://img.shields.io/packagist/v/bem/bh.svg?style=flat)](https://packagist.org/packages/bem/bh)\n [![Total Downloads](https://img.shields.io/packagist/dm/bem/bh.svg?style=flat)](https://packagist.org/packages/bem/bh)\n\n[![Build Status](https://img.shields.io/travis/bem/bh-php.svg?style=flat)](https://travis-ci.org/bem/bh-php)\n [![Coverage Status](https://img.shields.io/coveralls/bem/bh-php.svg?style=flat)](https://coveralls.io/r/bem/bh-php)\n [![Dependency Status](https://img.shields.io/gemnasium/bem/bh-php.svg?style=flat)](https://gemnasium.com/bem/bh-php)\n\nBH is a processor that converts BEMJSON to HTML. Or in other words a template engine.\n\nWorks with `PHP 5.4+` (doesn't work with `HHVM` 'cause it lacks a lot of required functionality)\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Friendly Packages](#friendly-packages)\n- [Usage](#usage)\n- [Conversion](#conversion)\n\n## Friendly Packages\n\n- [Project Stub with BH.PHP tech](https://github.com/bem/project-stub/tree/php-bem-bh)\n- [BEM Core Library with BH.PHP templates](https://github.com/zxqfox/bem-core/tree/feature/php-bh-templates%40v2) - tmp branch\n- [BEM Components Library with BH.PHP templates](https://github.com/zxqfox/bem-components/tree/feature/php-bh-templates%40v2) - tmp branch\n\n## Installation\n\n### Via composer\n\nExecute in your shell:\n```\nphp composer.phar require bem/bh\n```\nor (if you have `composer` in your path)\n```\ncomposer require bem/bh\n```\n\nAnd use in your code:\n```php\nrequire \"vendor/autoload.php\";\n$bh = new \\BEM\\BH();\n// ...\n```\n\n### Manual installation\n\nDownload using `git` (execute this in your shell):\n```\n# via git\ngit clone https://github.com/bem/bh-php.git ./vendor/bem/bh\n```\n\nUsing `wget` and `tar`:\n```\n# via wget + tar\nwget https://github.com/bem/bh-php/archive/master.tar.gz # download archive\ntar -xzvf master.tar.gz --exclude=tests        # extract\n[ ! -d ./vendor/bem ] \u0026\u0026 mkdir ./vendor/bem -p # create vendor director\nmv ./bh-php-master ./vendor/bem/bh             # move library to vendor\nrm master.tar.gz                               # cleanup\n```\n\nOr 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).\n\nAnd use in your code:\n```php\n// manual installation\nrequire \"vendor/bem/bh/index.php\";\n$bh = new \\BEM\\BH();\n// ...\n```\n\n## Usage\n\nBH files within a project have `.bh.php` suffix (for example, `page.bh.php`). The file is formed in CommonJS-like format:\n\n```php\nreturn function ($bh) {\n    $bh-\u003ematch(/*...*/);\n    // ...\n};\n```\n\nTo load this file format use include and run technique:\n```php\n// Instantiate BH object\n$bh = new \\BEM\\BH();\n\n// Load and apply matchers to BH object in $bh\n$fn = include('file.bh.php');\n$fn($bh); // done. and nothing in global\n\n// ...\n```\n\nThis allows you to have several instances at the moment:\n```php\n$bh1 = new \\BEM\\BH();\n$bh2 = new \\BEM\\BH();\n\n// load matchers\n$indexMatchers = include('bundles/index/index.bh.php');\n$mergedMatchers = include('bundles/merged/merged.bh.php');\n\n// apply them\n$indexMatchers($bh1); // bh1 now contains matchers for index page only\n$mergedMatchers($bh2); // bh2 now contains all matchers\n\n// use it with the same bemjson data\n$bh1-\u003eapply($bemjson);\n$bh2-\u003eapply($bemjson);\n```\n\nUse `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.\n\nCommon use case:\n\n```php\nrequire \"vendor/autoload.php\";\n$bh = new \\BEM\\BH();\n$bh-\u003ematch('button', function ($ctx) {\n    $ctx-\u003etag('button');\n});\n\n$bh-\u003eprocessBemJson([ 'block' =\u003e 'block' ]);\n// [ 'block' =\u003e 'button', 'mods' =\u003e new Mods(), 'tag' =\u003e 'button' ]\n\n$bh-\u003eapply([ 'block' =\u003e 'button' ]);\n// '\u003cbutton class=\"button\"\u003e\u003c/button\u003e'\n```\n\n## Conversion\n\nWorking functions for BEMJSON are **templates**. Use `match` method to declare templates. Logic of BEMJSON conversion is declared in a function body.\n\nThere are two arguments provided to a template function:\n* `$ctx` – instance of `\\BEM\\Context` class;\n* `$json` – instance of `\\BEM\\Json` class (current BEMJSON tree node).\n\n*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-\u003ejson()` method).\n\nSyntax:\n\n```php\n/**\n * Register matchers\n * @param string|array $expression bem css expression\n * @param closure [$matcher]\n * @return \\BEM\\BH\n */\n$bh-\u003ematch(/*string*/ $expression, function (\\BEM\\Context $ctx, \\BEM\\Json $json) {\n    // ... actions\n});\n\n// or...\n$bh-\u003ematch([/*string*/ $expression], function (\\BEM\\Context $ctx, \\BEM\\Json $json) {\n    // ... actions\n});\n\n// or...\n$bh-\u003ematch(/*array*/ $matchers = [\n    \"$expression\" =\u003e function(\\BEM\\Context $ctx, \\BEM\\Json $json) {\n        // ... actions\n    },\n    // ... more matchers\n]);\n```\n\nLook 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).\n\n## License\n\n[The MIT Licence](LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbem%2Fbh-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbem%2Fbh-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbem%2Fbh-php/lists"}