djl997/blade-shortcuts
Composer 安装命令:
composer require djl997/blade-shortcuts
包简介
Blade Shortcuts is a library of handy Laravel Blade Directives.
README 文档
README
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
- Arrays
- Boolean
- Config
- Dates
- Filesizes
- Fluent Strings
- Money
- Nl2br
- Not empty
- Not isset
- Percentage
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, ES supported).
@date() <!-- February 8, 2024 --> @date(time()) <!-- February 8, 2024 --> @date(date('Y-m-d')) <!-- February 8, 2024 -->
Other options:
@date(now()->subHours(20)) <!-- February 14, 2024 --> @date(now()->subHours(20), 'dateOrDiff') <!-- 20 hours ago --> @date(now()->subWeek()) <!-- February 8, 2024 --> @date(now()->subWeek(), 'dateOrDiff') <!-- February 8, 2024 -->
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 <!-- February 8, 2024 3:04 PM --> @time <!-- 3:04 PM --> @year <!-- 2024 --> @month <!-- February --> @day <!-- Tuesday -->
You even can add a custom date to datetime, time, year, month or day, for example:
@day(now()) <!-- Thursday --> @year('2024-02-08') <!-- 2024 -->
Day of Week, Day of Month, Day of Year
In some cases you need the x day of week, month or year.
@dayOfYear <!-- 177 --> @dayOfMonth <!-- 25 --> @dayOfWeek <!-- 2 -->
Or generate it based on a value:
@dayOfWeek('2024-06-25') <!-- 2 --> @dayOfWeek(now()->subDay()) <!-- 1 --> @dayOfYear($user->updated_at) <!-- 177 -->
Carbon Cascades
If you want to display a certain amount of time in human readable format, try out the new cascade directives. For example, convert 125 minutes to a readable format:
@cascadeFromMinutes(125) <!-- 2h 5m --> @cascadeFromHours(146) <!-- 6d 2h -->
Change the time unit
If you set the time unit (2nd item in the array), the cascade will cascade max to the given unit. In the example below, we have 1530 minutes, divided into hours of 60 minutes:
@cascadeFromMinutes(1530) <!-- 1d 1h 30m --> @cascadeFromMinutes([1530, ['hour' => 60]]) <!-- 25h 30m, (Please notice the use of an array!) -->
CarbonInterval
The example above also means you can tweak the CarbonInterval. Suppose you have a project that requires 125 hours of work and you can allocate 30 hours per day for it. How many days will it take to complete the project? We use the @cascadeFromHours directive to calculate this value:
@cascadeFromHours([125, ['day' => 30]]) <!-- 4d 5h -->
Filesize
@filesize(2145) <!-- 2 kB --> @filesizemb(124588) <!-- <1 MB --> @filesizegb(1198466000) <!-- 1,1 GB -->
nl2br
How to display input from a textarea in a read-only situation? Maybe you use {!! $comment !!} to get unexcaped data. In this way, you loose the XSS prevention, so maybe you sacrifice the newlines if the risk is too high. Now, that is no longer necessary: use the @nl2br directive.
@nl2br('Your view will show newlines.\n\n Very intuitive.') <!-- Your view will show newlines. Very intuitive. --> @nl2br('Your possible unsafe HTML code <script>alert('Hello world')</script>\n will not execute.') <!-- Your possible unsafe HTML code <script>alert('Hello world')</script> will not execute. -->
Not Empty, inverse of @empty
@notEmpty(1) I'm not empty. @endNotEmpty
Not set, inverse of @isset
@notSet($notSetVariable) I'm not set. @endNotSet
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% -->
Simple Money
@simpleMoney() <!-- €0.00 --> @simpleMoney(.99) <!-- €0.99 --> @simpleMoney(112327.20) <!-- €112,327.20 --> @simpleMoney(112327.20, 'JPY') <!-- ¥112,327.20 --> @simpleMoney(112327.20, 'USD', 'es') <!-- 112.327,20 US$ -->
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.
djl997/blade-shortcuts 适用场景与选型建议
djl997/blade-shortcuts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.05k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2022 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「blade」 「blade-directives」 「laravel-blade」 「laravel-directives」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 djl997/blade-shortcuts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 djl997/blade-shortcuts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 djl997/blade-shortcuts 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of Blade directives for use with Roots Sage.
Comprehensive Laravel formatter package for money, percentages, file sizes, phone numbers, credit cards, durations, and more with Blade directives and helper functions
Advanced permission manager for Laravel.
Alfabank REST API integration
A set of Blade directives for use with Roots Sage.
A comprehensive Laravel package for advanced permission management. Features RBAC, granular permissions, entity-specific abilities, global groups, audit logging, and seamless Laravel integration.
统计信息
- 总下载量: 1.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 41
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-01