PHP code example of micropackage / classnames

1. Go to this page and download the library: Download micropackage/classnames 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/ */

    

micropackage / classnames example snippets


use Micropackage\ClassNames\ClassNames;

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


/**
 * Example WordPress template using ACF
 */

use Micropackage\ClassNames\ClassNames;

$text_color = get_filed( 'text-color' );

$classnames = new ClassNames(
	'main-hero',
	[
		'has-background'          => get_filed( 'has-background' ), // Conditionally add background class
		"has-{$text_color}-color" => $text_color, // Only add color class if color is not null
	]
);

$classnames = new ClassNames( 'foo' );

if ( is_bar() ) {
	$classnames->add( 'bar', [ 'baz' => is_baz() ] );
}

$classnames = new ClassNames( 'foo', 'bar', 'baz', 'duck' );

if ( ! is_bar() ) {
	$classnames->remove( 'bar', [ 'baz', 'duck' ] );
}

$classnames = new ClassNames( 'foo', [ 'bar' => true, 'baz' => false ] );

$result = $classnames->build(); // => 'foo bar'

$classnames = new ClassNames( 'foo', [ 'bar' => true, 'baz' => false ] );

$result = $classnames->buildAttribute( ' ', ' tabndex="-1"'); // => ' class="foo bar" tabindex="-1"'

$result = ClassNames::get( 'foo', [ 'bar' => true, 'baz' => false ] ); // => 'foo bar'

$result = ClassNames::getAttribute(
	'foo',
	[
		'bar' => true,
		'baz' => false
	],
	[
		'before' => 'prefix ',
		'after'  => ' sufix',
	]
); // => 'prefix class="foo bar" sufix'

ClassNames::print( 'foo', [ 'bar' => true, 'baz' => false ] ); // echoes 'foo bar'

ClassNames::getAttribute(
	'foo',
	[
		'bar' => true,
		'baz' => false
	],
	[
		'before' => 'prefix ',
		'after'  => ' sufix',
	]
); // echoes 'prefix class="foo bar" sufix'