PHP code example of kagagnon / bem-php

1. Go to this page and download the library: Download kagagnon/bem-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kagagnon / bem-php example snippets



/**
 * Separator between block and element
 */
KAGagnon\BemPhp\Helpers\BemHelper::$element_separator = '__';

/**
 * Separator between modifer and block/element
 */
KAGagnon\BemPhp\Helpers\BemHelper::$modifier_separator = '--';

/**
 * Should @bem create a tag element
 */
KAGagnon\BemPhp\Helpers\BemHelper::$create_tag = false;

/**
 * If create_tag is true, what's the default tag name
 */
KAGagnon\BemPhp\Helpers\BemHelper::$default_tag = 'div';

/**
 * Prefix of BEM classes.
 */
KAGagnon\BemPhp\Helpers\BemHelper::$block_prefix = '';


BemHelper::startBlock( 'block' );
    BemHelper::startBlock( 'other-block' );
    BemHelper::endBlock();
BemHelper::endBlock();


BemHelper::startBlock( 'cup' ); // Init Block "cup"
    BemHelper::bemClass(); // Generate : cup
    BemHelper::bemClass( [ 'glass' ] ); // Generate : cup cup--glass

    BemHelper::startBlock( 'spoon' ); // Init Block "spoon"
        BemHelper::bemClass(); // Generate : spoon
        BemHelper::bemClass( [ 'metallic', 'cold' ] ); // Generate : spoon spoon--metallic spoon--cold
        BemHelper::bemClass( 'sugar' ); // Generate : spoon__sugar
        BemHelper::bemClass( 'sugar', 'half-tea-spoon' ); // Generate : spoon__sugar spoon__sugar--half-tea-spoon
        BemHelper::bemClass( 'spoon-holder', '', 1 ) // Generate : cup__spoon-holder
    BemHelper::EndBlock();

    BemHelper::bemClass( 'tea' ); // Generate : cup__tea
    BemHelper::bemClass( 'coffee' ); // Generate : cup_coffee
    BemHelper::bemClass( 'coffee' , 'with-sugar' ); // Generate : cup__coffee cup__coffee--with-sugar
    BemHelper::bemClass( 'coffee' , [ 'with-sugar', 'with-milk'] ); // Generate : cup__coffee cup__coffee--with-sugar cup__coffee--with-milk
    BemHelper::bemClass( 'coffee' , 'with-sugar with-milk no-foam' ); // Generate : cup__coffee cup__coffee--with-sugar cup__coffee--with-milk cup__coffee--no-foam
BemHelper::EndBlock();

BemHelper::bemClass( 'coffee', [
    'with-sugar' => $has_sugar == true
] );

BemHelper::startBlock( 'cup', 'div', [
    '_modifiers' => [
        'blue',
        'is-cold' => !$has_hot_beverage,
        'is-empty' => $finished
    ]
] );

 BemHelper::startBlock( 'article' ) 


// We assume `create_tag` is set to true

BemHelper::startBlock( 'block' ) // <div class="block">
BemHelper::endBlock()            // </div>

BemHelper::startBlock( 'block', 'article' ) // <article class="block">
BemHelper::endBlock()                       // </article>

BemHelper::startBlock( 'block', 'quote', [ 'data-inspiration', 'class' => 'js-action' ] ) // <quote class="js-action block" data-inspiration >
BemHelper::endBlock()                                                                     //</quote>

BemHelper::startBlock( 'block', [ 'id' => "anchor" ] ) // <div class="block" id="anchor">
BemHelper::endBlock()                                  // </div>

BemHelper::startBlock( 'block', [ 'id' => "anchor", '_modifiers' => 'modifier' ] ) // <div class="block block--modifier" id="anchor">
BemHelper::endBlock()                                  // </div>

BemHelper::startBlock( 'block', [ '_modifiers' => [ 'modifier1', 'modifier2' ] ] ) // <div class="block block--modifier1 block--modifier2">
BemHelper::endBlock()                                  // </div>

shell
$ composer install kagagnon/bem-php