Skip to content

CorwinDev/blade-shortcuts

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blade Shortcuts

Latest Version on Packagist Total Downloads Build Status Software License

Blade Shortcuts is a library of clever Blade Directives as listed below. The goal is to have less repetitive (base) logic in your Blade Views, overall shorter code and better readability.

Requirements

Blade Shortcuts requires PHP 8+ and Laravel 6+.

Installation

You can install the package via composer:

composer require djl997/blade-shortcuts

Usage

After installation, all directives should be usable immediately. If something goes wrong at first use or after an update, php artisan view:clear should clear the issue.

Contents

App Name

@appname <!-- Laravel, default APP_NAME in .env file -->

Boolean

@boolean(true) <!-- true -->
@boolean(false) <!-- false -->

Config

@config('config-file.key') <!-- anything -->

Dates

Automatically translate dates in the correct localized format (currently only EN, NL, DE supported):

  • EN: November 8, 2022
  • NL: 8 november 2022
  • DE: 8. November 2022
@date(time()) <!-- November 8, 2022 -->
@date(date('Y-m-d')) <!-- November 8, 2022 -->

Other options:

@date(now()->subHours(20)) <!-- November 14, 2022 -->
@date(now()->subHours(20), 'dateOrDiff') <!-- 20 hours ago -->
@date(now()->subWeek()) <!-- November 8, 2022 -->
@date(now()->subWeek(), 'dateOrDiff') <!-- November 8, 2022 -->

If the time difference is more than 23 hours, ‘dateOrDiff’ will automatically show the date in a localized format instead of ‘x time ago’ or ‘in x time’. You can adjust this threshold in the config file: php artisan vendor:publish --tag=blade-shortcuts-config.

Try shortcuts for datetime, time, year, month or day (also in the correct localized format):

@datetime <!-- November 8, 2022 3:04 PM -->
@time <!-- 3:04 PM -->
@year <!-- 2022 -->
@month <!-- November -->
@day <!-- Tuesday -->

You even can add a custom date to datetime, time, year, month or day, for example:

@day(now())
@year('2022-11-08')

Cascades

If you want to display a certain amount of time in human readable format, try out the new cascade directives. For example:

@cascadeFromMinutes(125) <!-- 2h 5m -->
@cascadeFromHours(146) <!-- 6d 2h -->

Change the Carbon Interval. For example, if a company has 30 working hours available per day, how long will a project of 125 hours take?

@cascadeFromHours([125, ['day' => 30]]) <!-- 4d 5h -->

Filesize

@filesize(2145) <!-- 2 kB -->
@filesizemb(124588) <!-- <1 MB -->
@filesizegb(1198466000) <!-- 1,1 GB -->

Not Empty, inverse of @empty

@notEmpty(1)
    I'm not empty.
@endNotEmpty

Not Isset, inverse of @isset

@notIsset($notSetVariable)
    I'm not set.
@endNotIsset

Percentages

@percentage(1) <!-- 100% -->
@percentage(0.055) <!-- 5.5% -->
@percentage(100) <!-- 100% -->
@percentage(50) <!-- 50% -->
@percentage(0.5) <!-- 50% -->
@percentage(0.505) <!-- 50.5% -->
@percentage(-5) <!-- -5% -->

Helpers

Arrays

<?php $array = ['Tailwind', 'Alpine', 'Laravel', 'Livewire']; ?>

<!-- Before -->
{{ Illuminate\Support\Arr::join($array, ', ', ' and ') }}

<!-- After -->
@arr(join($array, ', ', ' and '))  

<!-- Result -->
Tailwind, Alpine, Laravel and Livewire

Find all available methods in Laravel Docs.

Fluent strings

<!-- Before -->
{{ Illuminate\Support\Str::of('    laravel    framework    ')->squish() }}

<!-- After -->
@str(of('    laravel    framework    ')->squish())  

<!-- Result  -->
laravel framework

Find all available methods in Laravel Docs.

Publish config

php artisan vendor:publish --tag=blade-shortcuts-config

Changelog

Please see GitHubs releases section for more information on what has changed recently.

Contributing

Contributions are welcome.

License

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

About

Blade Shortcuts is a library of handy Laravel Blade Directives.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%