|
1 | 1 | # classnames-php |
2 | | -PHP port of classnames |
| 2 | +PHP port of the JavaScript classNames utility. https://github.com/JedWatson/classnames |
| 3 | + |
| 4 | +## Installation |
| 5 | + |
| 6 | +``` |
| 7 | +composer require cjstroud/classnames-php |
| 8 | +``` |
| 9 | + |
| 10 | +The classNames can be accessed anywhere. |
| 11 | + |
| 12 | +``` |
| 13 | +classNames('foo', ['bar' => true]); // 'foo bar' |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +The `classNames` function takes any number of arguments which can be a string or array. |
| 19 | +When using an array, if the value associated with a given key is falsy, that key won't be included in the output. |
| 20 | +If no value is given the true is assumed. |
| 21 | + |
| 22 | +``` |
| 23 | +classNames('foo'); // 'foo' |
| 24 | +classNames(['foo' => true]); // 'foo' |
| 25 | +classNames('foo', ['bar' => false, 'baz' => true]); // 'foo baz' |
| 26 | +classNames(['foo', 'bar' => true]) // 'foo bar' |
| 27 | +
|
| 28 | +// Falsy values get ignored |
| 29 | +classNames('foo', null, 0, false, 1); // 'foo 1' |
| 30 | +``` |
| 31 | + |
| 32 | +Objects and functions will be ignored, unless the object has __toString() function. |
| 33 | +If it does that will be called and the string value used. |
| 34 | + |
| 35 | +``` |
| 36 | +class ExampleObject { |
| 37 | + function __toString() |
| 38 | + { |
| 39 | + return 'bar'; |
| 40 | + } |
| 41 | +} |
| 42 | +
|
| 43 | +classNames('foo', function() {}, new stdClass(), new ExampleObject()); // 'foo bar' |
| 44 | +``` |
| 45 | + |
| 46 | +## Laravel Blade |
| 47 | + |
| 48 | +``` |
| 49 | +<div class="{{ classNames('foo', ['bar' => true]) }}"></div> |
| 50 | +
|
| 51 | +<div class="foo bar"></div> |
| 52 | +``` |
| 53 | + |
| 54 | +## License |
| 55 | + |
| 56 | +[MIT](LICENSE). Copyright (c) 2017 Chris Stroud. |
0 commit comments