zicht/classes

PHP utility for conditionally joining CSS classes.

2.1.0 2021-12-13 08:54 UTC

This package is auto-updated.

Last update: 2024-10-17 14:57:35 UTC


README

Utility function for conditionally joining CSS classes.

Build Status Code Coverage Scrutinizer Code Quality

Installing

composer require zicht/classes

Purpose

When building a component (or any piece of HTML, really), it's quite common to render or not render certain CSS classes based on certain variables. If you manually concatenate the class names, it quickly becomes a mess of if / else or ternary operators. This function helps to keep it readable.

Usage

HtmlClassHelper::classes('art-vandelay'); ⇒ 'art-vandelay'
HtmlClassHelper::classes(['art-vandelay', 'kramerica']); ⇒ 'art-vandelay  kramerica'
HtmlClassHelper::classes(['art-vandelay' => true, 'kramerica' => false]); ⇒ 'art-vandelay'
HtmlClassHelper::classes('art-vandelay', ['kramerica' => false, 'kel-varnsen' => true]); ⇒ 'art-vandelay  kel-varnsen'

Credits

This function was created because the JS equivalent classnames was nice to use in React.

Maintainer