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.
Blade Shortcuts requires PHP 8+ and Laravel 6+.
You can install the package via composer:
composer require djl997/blade-shortcuts
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.
- App Name
- Boolean
- Config
- Dates
- date
- datetime
- time
- year
- month
- day
- cascadeMinutes
- cascadeHours
- cascadeDays
- cascadeWeeks
- cascadeMonths
- Filesizes
- Not empty
- Not isset
- Percentage
- Helpers
@appname <!-- Laravel, default APP_NAME in .env file -->
@boolean(true) <!-- true -->
@boolean(false) <!-- false -->
@config('config-file.key') <!-- anything -->
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')
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(2145) <!-- 2 kB -->
@filesizemb(124588) <!-- <1 MB -->
@filesizegb(1198466000) <!-- 1,1 GB -->
@notEmpty(1)
I'm not empty.
@endNotEmpty
@notIsset($notSetVariable)
I'm not set.
@endNotIsset
@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% -->
<?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.
<!-- Before -->
{{ Illuminate\Support\Str::of(' laravel framework ')->squish() }}
<!-- After -->
@str(of(' laravel framework ')->squish())
<!-- Result -->
laravel framework
Find all available methods in Laravel Docs.
php artisan vendor:publish --tag=blade-shortcuts-config
Please see GitHubs releases section for more information on what has changed recently.
Contributions are welcome.
The MIT License (MIT). Please see License File for more information.