Last active
December 17, 2015 08:59
-
-
Save ZeeDev/5583629 to your computer and use it in GitHub Desktop.
BEM.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* BEM | |
* experimental function to output BEM style class names from strings and/or arrays | |
* @param string|array $b block | |
* @param string|array $e element | |
* @param string|array $m modifier | |
* @return string class names | |
* | |
* eg: | |
* BEM('page', 'title') > page_title | |
* BEM('btn', 0, 'primary') > btn btn--primary | |
* BEM(array('node', 'page-node'), 'title') > node_title page-node_title | |
* BEM(array('node', 'page-node'), 0, array('teaser', 'nested')) > node page-node node--teaser page-node--teaser node--nested page-node--nested ... fiou | |
*/ | |
function BEM($b = array(), $e = array(), $m = array()) { | |
$element_separator = '_'; | |
$modifier_separator = '--'; | |
$classes = array(); | |
$modifiers = array(); | |
// Every arguments could be a string or an array | |
if(is_string($b)) $b = array($b); | |
if(is_string($e)) $e = array($e); | |
if(is_string($m)) $m = array($m);; | |
foreach($b as $block) { | |
// Block only | |
if(empty($e)) { | |
$classes[] = $block; | |
// Element only | |
} else { | |
foreach($e as $element) { | |
$classes[] = $block.$element_separator.$element; | |
} | |
} | |
} | |
// append modifiers after classes | |
foreach($m as $modifier) { | |
foreach($classes as $classname) { | |
$modifiers[] = $classname.$modifier_separator.$modifier; | |
} | |
} | |
return implode(' ', array_merge($classes, $modifiers)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment