PHP code example of dobaniashish / clsx

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

    

dobaniashish / clsx example snippets


use Dobaniashish\Clsx\Clsx;

// String values.
Clsx::attrs(['foo' => 'bar', 'baz' => 'qux']);
//=> 'foo="bar" baz="qux"'

// Arrays values.
Clsx::attrs(['foo' => 'bar', 'baz' => ['qux', 'quux']]);
//=> 'foo="bar" baz="qux quux"'

// Arrays values with conditions.
Clsx::attrs([
	'foo' => 'bar',
	'baz' => [
		'qux' => true,
		'quux' => false,
	]
]);
//=> 'foo="bar" baz="qux"'

// Attribute names only.
Clsx::attrs(['foo', 'bar']);
//=> 'foo bar'

// Attribute names only with conditions.
Clsx::attrs(['foo' => true, 'bar' => false]);
//=> 'foo=""'

// Arrays values with all falsy conditions.
Clsx::attrs([
	'foo' => 'bar',
	'baz' => [
		'qux' => false,
		'quux' => false,
	]
]);
//=> 'foo="bar"'

// Arrays values with all falsy conditions but keep attribute name.
Clsx::attrs([
	'foo' => 'bar',
	'baz' => Clsx::value([
		'qux' => false,
		'quux' => false,
	]) ?: true
]);
//=> 'foo="bar" baz'


use Dobaniashish\Clsx\Clsx;

$button_attrs = [
	'id' => 'button-id',
	'class' => [
		'button',
		'button-success' => true,
	],
	"disabled" => false,
];

use Dobaniashish\Clsx\Clsx;

// String values.
Clsx::value(['foo', 'bar']);
//=> 'foo bar'

// Arrays value with conditions.
Clsx::value(['foo' => true, 'bar' => true, 'baz' => false]);
//=> 'foo bar'


use Dobaniashish\Clsx\Clsx;

$button_classes = Clsx::value([
	'button',
	'button-success' => true,
]);


use Dobaniashish\Clsx\Clsx;

$button_attrs = [
	'id' => 'button-id',
	'class' => [
		'button',
	],
];

$button_attrs = Clsx::merge($button_attrs, [
	'class' => [
		'button-success' => true,
	],
]);


use Dobaniashish\Clsx\Clsx;

// Button statuses.
$button_status = 'success';
$button_size = 'large';
$button_disabled = false;
$button_hide_border = true;

// Button attributes array.
$button_attrs = [
	'id' => 'button-id',
	'class' => [
		'button',
		'button-success' => $button_status === 'success',
		'button-danger' => $button_status === 'danger',
		"button-{$button_size}" => !!$button_size,
	],
	"disabled" => $button_disabled,
	"style" => [
		'color: red;',
		'border: none;' => $button_hide_border,
	],
];

// Add directly to array.
$button_attrs['class']['button-danger'] = false;

// Merge new attribute array.
$button_attrs = Clsx::merge($button_attrs, [
	'class' => [
		// Pre process value.
		Clsx::value([
			'button-merged'
		])
	]
]);

Clsx::attrs([
	esc_name('foo') => esc_value('bar'),
	esc_name('baz') => esc_value(Clsx::value([
		'qux' => false,
		'quux' => false,
	])) ?: true
]);