Skip to content

cstro/classnames-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

classnames-php

Build Status

PHP port of the JavaScript classNames utility. https://github.com/JedWatson/classnames

Installation

composer require cjstroud/classnames-php

The classNames can be accessed anywhere.

classNames('foo', ['bar' => true]); // 'foo bar'

Usage

The classNames function takes any number of arguments which can be a string or array. When using an array, if the value associated with a given key is falsy, that key won't be included in the output. If no value is given the true is assumed.

classNames('foo'); // 'foo'
classNames(['foo' => true]); // 'foo'
classNames('foo', ['bar' => false, 'baz' => true]); // 'foo baz'
classNames(['foo', 'bar' => true]) // 'foo bar'

// Falsy values get ignored
classNames('foo', null, 0, false, 1); // 'foo 1'

Objects and functions will be ignored, unless the object has __toString() function. If it does that will be called and the string value used.

class ExampleObject {
    function __toString()
    {
        return 'bar';
    }
}

classNames('foo', function() {}, new stdClass(), new ExampleObject()); // 'foo bar'

Laravel Blade

<div class="{{ classNames('foo', ['bar' => true]) }}"></div>

<div class="foo bar"></div>

License

MIT. Copyright (c) 2017 Chris Stroud.

About

A simple php utility for conditionally joining classNames together, based on https://github.com/JedWatson/classnames

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy