tautvydasr / blade-class-names
Package for handling html classes output based on conditions
Installs: 4 558
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: ^7.1.3|^8.0
- illuminate/support: ^5.6 || 6.* || 7.* || 8.*
Requires (Dev)
- phpunit/phpunit: ^7.2|^9.5
README
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.
Installation
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); ...
Usage
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.
Local setup
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
Tests
Run phpunit tests:
./vendor/bin/phpunit
License
Package is free to use and is licensed under the MIT license