All Projects → bem → bh-php

bem / bh-php

Licence: MIT License
PHP port of https://github.com/bem/bh. It's cool thing but better use this:

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to bh-php

Bem Xjst
bem-xjst (eXtensible JavaScript Templates): declarative template engine for the browser and server
Stars: ✭ 115 (+248.48%)
Mutual labels:  template-engine, bem
Fatfree
A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
Stars: ✭ 2,504 (+7487.88%)
Mutual labels:  template-engine, php-template
bemer
Template engine. BEMJSON to HTML processor.
Stars: ✭ 33 (+0%)
Mutual labels:  bem, bemjson
view
Template Engine For AdonisJS
Stars: ✭ 13 (-60.61%)
Mutual labels:  template-engine
typesafe-templates
Template engine that leverages JSX to generate JavaScript code from TypeScript code files rather than text templates.
Stars: ✭ 27 (-18.18%)
Mutual labels:  template-engine
yatpl
Yet Another Template Engine 🚀
Stars: ✭ 14 (-57.58%)
Mutual labels:  template-engine
Contemplate
Contemplate: Fast, extendable object-oriented and light-weight Template Engine for PHP, Python, Node.js, Browser and XPCOM/SDK JavaScript
Stars: ✭ 15 (-54.55%)
Mutual labels:  template-engine
ml-stack-nav
Customizable, responsive, accessible, easy-to-use multi-level stack navigation menu with slide effect.
Stars: ✭ 20 (-39.39%)
Mutual labels:  bem
thera
A template engine for Scala
Stars: ✭ 49 (+48.48%)
Mutual labels:  template-engine
moustachu
Mustache templating for Nim
Stars: ✭ 58 (+75.76%)
Mutual labels:  template-engine
kotlin-html
A library to generate HTML in Kotlin.
Stars: ✭ 23 (-30.3%)
Mutual labels:  template-engine
lua-resty-aries
openresty and lua multi-function template
Stars: ✭ 47 (+42.42%)
Mutual labels:  template-engine
hulk-template
为 CodeIgniter 框架增加视图继承功能,不改变原有视图编写方式,无缝增加视图继承功能。
Stars: ✭ 17 (-48.48%)
Mutual labels:  template-engine
shaven
DOM building utility & Template engine based on JsonML + syntax sugar
Stars: ✭ 66 (+100%)
Mutual labels:  template-engine
pointcss
A lightweight and flexible CSS Framework for building modern responsive web apps
Stars: ✭ 33 (+0%)
Mutual labels:  bem
kirby-blade
Enable Laravel Blade Template Engine for Kirby 3
Stars: ✭ 20 (-39.39%)
Mutual labels:  template-engine
katapult
Kickstart Rails development!
Stars: ✭ 21 (-36.36%)
Mutual labels:  template-engine
MulleScion
🌱 A modern template engine for Objective C
Stars: ✭ 14 (-57.58%)
Mutual labels:  template-engine
bh
BH template engine
Stars: ✭ 68 (+106.06%)
Mutual labels:  bemjson
essential-templating
A set of templating libraries.
Stars: ✭ 21 (-36.36%)
Mutual labels:  template-engine

bh-php

![Gitter](https://badges.gitter.im/Join 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

Installation

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 https://github.com/bem/bh-php.git ./vendor/bem/bh

Using wget and 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

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();
// ...

Usage

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

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

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('file.bh.php');
$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/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.

Common use case:

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.

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

Syntax:

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

License

The MIT Licence.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].