micropackage/classnames
Composer 安装命令:
composer require micropackage/classnames
包简介
Simple PHP utility class for conditionally joining class names together.
README 文档
README
🧬 About ClassNames
This package contains simple utility class for conditionally joining html classNames. It was inspired by the JavaScript classnames package.
💾 Installation
composer require micropackage/classnames
🕹 Usage
The Micropackage\ClassNames\ClassNames class constructor takes any number of arguments which can be a string or an array. String arguments and values of string arrays will be used unconditionally. If an argument is an array with string keys, keys will be used as classnames if the value associated with a given key is truthy.
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'
Full example
<?php /** * 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 ] ); ?> <div class="<?php echo $classnames; ?>"> <!-- (...) --> </div>
Methods
add
Adds classNames to the current set. Accepts any number of arguments, just like the constructor.
$classnames = new ClassNames( 'foo' ); if ( is_bar() ) { $classnames->add( 'bar', [ 'baz' => is_baz() ] ); }
Returns array
All included classnames.
remove
Removes classNames from the current set. Accepts any number of arguments which can be a string or an array of strings.
$classnames = new ClassNames( 'foo', 'bar', 'baz', 'duck' ); if ( ! is_bar() ) { $classnames->remove( 'bar', [ 'baz', 'duck' ] ); }
Returns array
All included classnames.
build
Creates string from current classNames set.
$classnames = new ClassNames( 'foo', [ 'bar' => true, 'baz' => false ] ); $result = $classnames->build(); // => 'foo bar'
Returns string
buildAttribute
Creates string with HTML class attribute from current classNames set.
Params
string $before Optional prefix
string $after Optional suffix
$classnames = new ClassNames( 'foo', [ 'bar' => true, 'baz' => false ] ); $result = $classnames->buildAttribute( ' ', ' tabndex="-1"'); // => ' class="foo bar" tabindex="-1"'
Returns string
Static methods
ClassName::get
Accepts arguments like constructor and returns a className string. This is a short equivalent of creating an instance and calling $instance->build().
$result = ClassNames::get( 'foo', [ 'bar' => true, 'baz' => false ] ); // => 'foo bar'
Returns string
ClassName::getAttribute
Accepts arguments like constructor and returns a class attribute string. This is a short equivalent of creating an instance and calling $instance->buildAttribute().
It's possible to pass an array with keys before and/or after as one of arguments.
$result = ClassNames::getAttribute( 'foo', [ 'bar' => true, 'baz' => false ], [ 'before' => 'prefix ', 'after' => ' sufix', ] ); // => 'prefix class="foo bar" sufix'
Returns string
ClassName::print
Echoes the result of ClassName::get.
ClassNames::print( 'foo', [ 'bar' => true, 'baz' => false ] ); // echoes 'foo bar'
Returns void
ClassName::printAttribute
Echoes the result of ClassName::getAttribute.
ClassNames::getAttribute( 'foo', [ 'bar' => true, 'baz' => false ], [ 'before' => 'prefix ', 'after' => ' sufix', ] ); // echoes 'prefix class="foo bar" sufix'
Returns void
📦 About the Micropackage project
Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.
The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.
Micropackages are maintained by BracketSpace.
📖 Changelog
📃 License
GNU General Public License (GPL) v3.0. See the LICENSE file for more information.
© Credits
This package was inspired by the JavaScript classnames by Jed Watson.
micropackage/classnames 适用场景与选型建议
micropackage/classnames 是一款 基于 HTML 开发的 Composer 扩展包,目前已累计 19.96k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 micropackage/classnames 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 micropackage/classnames 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 19.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2021-04-12