Simple package which helps to handle conditional html classes in blade templates.
NOTICE: If Laravel 8 or later is used consider using native @class
directive instead.
First of all require this package in composer:
composer require tautvydasr/blade-class-names
Finally, add service provider to providers
array in your config/app.php
file for Laravel:
'providers' => [
// ...
ClassNames\ClassNamesServiceProvider::class,
// ...
],
or register service provider in bootstrap/app.php
if using Lumen:
...
$app->register(\ClassNames\ClassNamesServiceProvider::class);
...
Basically package allows you to simplify conditional classes situations like this
<a href="#" class="menu-item{{ $loop->first ? ' first-item' : '' }}{{ request()->routeIs('foo') ? ' active' : '' }}">
...
</a>
to this using blade directive @classNames()
.
<a href="#" class="@classNames('menu-item', ['first-item' => $loop->first, 'active' => request()->routeIs('foo')])">
...
</a>
For Lumen users it can be accessed via app()
helper or facade.
<a href="#" class="app('classnames')->render('menu-item', ['first-item' => $loop->first, 'active' => request()->routeIs('foo')])">
...
</a>
<a href="#" class="\ClassNames\ClassNamesFacade::render('menu-item', ['first-item' => $loop->first, 'active' => request()->routeIs('foo')])">
...
</a>
Using facade approach make sure the line $app->withFacades();
is uncommented in bootstrap/app.php
file.
Copy example docker compose config file (optional):
cp docker-compose.yml.dist docker-compose.yml
Run docker containers (optional):
docker-compose up -d
Login to docker container where CONTAINER_ID
is your id (optional):
docker exec -ti CONTAINER_ID /bin/bash
Install dependencies using composer:
composer install
Run phpunit tests:
./vendor/bin/phpunit
Package is free to use and is licensed under the MIT license