okipa/laravel-html-helper

Useful helpers to dynamically generate clean HTML with Laravel.

2.0.0 2020-11-14 14:22 UTC

This package is auto-updated.

Last update: 2020-11-27 13:58:29 UTC


README

Laravel HTML Helper

Latest Stable Version Total Downloads Build Status Coverage Status License: MIT

Easily handle conditional HTML generation with the following helpers.

Found this package helpful? Please consider supporting my work!

Donate Donate

Compatibility

Laravel PHP Package
^7.* ^7.4 ^2.0
^5.8 ^7.2 ^1.3
^5.5 ^7.2 ^1.2
^5.5 ^7.1 ^1.0

Upgrade guide

Table of Contents

Installation

  • Install the package with composer:
composer require okipa/laravel-html-helper

API

html_classes

Calling this helper generates an HTML class tag encapsulating the given dynamic classes.

It accepts combination of strings, integers, arrays or null arguments.

@php
    $id = 17;
    $fullScreen = false;
    $darkMode = true;
@endphp
<div{{ html_classes(
    ['card', $id, 'text-left'],
    $fullScreen ? 'full-screen' : null,
    $darkMode ? ['bg-dark', 'text-white'] : null
) }}></div>
<div class="card 17 text-left bg-dark text-white"></div>

You can call app(Okipa\LaravelHtmlHelper\HtmlClasses)->toHtml($classes) if you want to avoid helper use.

html_attributes

Calling this helper generates dynamic HTML attributes, taking care about the given key-only, value-only or key-value combinations.

It accepts combination of strings, arrays or null arguments.

@php
    $dragAndDrop = true;
    $disabled = false;
@endphp
<div{{ html_attributes(
    ['data-confirm' => __('Are you sure you want to delete this line?')],
    $dragAndDrop ? 'data-drag-drop' : null,
    $disabled ? ['disabled', 'data-forbid-click'] : null,
    'required'
) }}></div>
<div data-confirm="Are you sure you want to delete this line?" data-drag-drop required></div>

You can call app(Okipa\LaravelHtmlHelper\HtmlAttributes)->toHtml($attributes) if you want to avoid helper use.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.