tautvydasr/blade-class-names

Package for handling html classes output based on conditions

1.3.1 2021-02-18 17:29 UTC

This package is auto-updated.

Last update: 2024-04-19 15:39:48 UTC


README

Build Status

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