Blade class names
, (*1)
Simple package which helps to handle conditional html classes in blade templates., (*2)
NOTICE: If Laravel 8 or later is used consider using native @class
directive instead., (*3)
Installation
First of all require this package in composer:, (*4)
composer require tautvydasr/blade-class-names
Finally, add service provider to providers
array in your config/app.php
file for Laravel:, (*5)
'providers' => [
// ...
ClassNames\ClassNamesServiceProvider::class,
// ...
],
or register service provider in bootstrap/app.php
if using Lumen:, (*6)
...
$app->register(\ClassNames\ClassNamesServiceProvider::class);
...
Usage
Basically package allows you to simplify conditional classes situations like this, (*7)
<a href="#" class="menu-item{{ $loop->first ? ' first-item' : '' }}{{ request()->routeIs('foo') ? ' active' : '' }}">
...
</a>
to this using blade directive @classNames()
., (*8)
<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., (*9)
<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., (*10)
Local setup
Copy example docker compose config file (optional):, (*11)
cp docker-compose.yml.dist docker-compose.yml
Run docker containers (optional):, (*12)
docker-compose up -d
Login to docker container where CONTAINER_ID
is your id (optional):, (*13)
docker exec -ti CONTAINER_ID /bin/bash
Install dependencies using composer:, (*14)
composer install
Tests
Run phpunit tests:, (*15)
./vendor/bin/phpunit
License
Package is free to use and is licensed under the MIT license, (*16)