rapidez/blade-directives
Composer 安装命令:
composer require rapidez/blade-directives
包简介
Expanded blade directives
关键字:
README 文档
README
This package adds blade directives that we found we needed in Laravel during development of Rapidez. Like @slots, which lets you define optional slots so your attributes->merge() always works. Or @includeFirstSafe which works the same as @includeFirst but will not throw errors if no template was found. All directives included within this package:
@attributes@includeFirstSafe@markdown@return@slots@slotdefault+@endslotdefault@includeCached
Installation
composer require rapidez/blade-directives
Directives
@attributes
The @attributes blade directive allows you to pass the attributes for a html element using an array. It's functionally the same as the $attributes of a blade component but you can use it outside of blade components!
Usage
<input @attributes(['type' => 'text', 'id' => 'test', 'name' => 'some_name'])/>
which will result in
<input type="text" id="test" name="some_name" />
@includeFirstSafe
The @includeFirstSafe blade directive works the same way that @includeFirst does however it will not throw an error if all templates do not exsist.
Outside of production mode it will alert about the missing templates however.
Usage
@includeFirstSafe(['custom.admin', 'admin'], ['status' => 'complete'])
@markdown
You can use the @markdown directive to transform markdown into html. Basically, {!! Str::markdown($text) !!} but in directive form.
Usage
@markdown($text)
@return
The @return blade directive simply stops any further processing of the current template
Usage
@return
Or only when a condition is true:
@return($someConditionIsTrue)
@slotdefault
When you've an optional slot this directive gives you a cleaner way of defining a fallback. Normally you do something like this:
@if ($slot->isEmpty()) This is default content if the slot is empty. @else {{ $slot }} @endif
Usage
@slotdefault('slot') This is default content if the slot is empty. @endslotdefault
@slots
The @slots blade directive is used within blade components.
It is used to define optional named slots which will be created if they are not passed.
Very useful if named slots might not always be passed but you want to use the attributes of this named slot
Usage
Within your blade component:
@slots(['optionalSlot', 'anotherSlot' => ['contents' => 'dummy text', 'attributes' => ['class' => 'bg-red-500']]]) <div {{ $attributes }}> {{ $slot }} <div {{ $optionalSlot->attributes }}>{{ $optionalSlot }}</div> <div {{ $anotherSlot->attributes->class('text-black') }}>{{ $anotherSlot }}</div> </div>
If you enter nothing and only load in the component without passing any named slots it will be
<div > <div ></div> <div class="bg-red-500 text-black">dummy text</div> </div>
but if you were to pass the named slots it would look like this:
<x-component> Regular slot text <x-slot:optionalSlot>Optional content</x-slot:optionalSlot> <x-slot:anotherSlot class="text-lg">Optional content</x-slot:anotherSlot> </x-component>
<div > Regular slot text <div >Optional content</div> <div class="text-lg text-black">Optional content</div> </div>
As you can see it has overwritten the class of the optional slot, but not the attributes->class()
If you only wish to change the text without changing attributes you can also pass them as attributes.
<x-component optionalSlot="Optional content" anotherSlot="Optional content"> Regular slot text </x-component>
<div > Regular slot text <div >Optional content</div> <div class="bg-red-500 text-black">Optional content</div> </div>
@includeCached
Just like @include but cached. Everything returned will be cached with Cache::flexible() for 5 minutes; and refreshed in the background until it expires after 24 hours. After that it will be refreshed as usual. The cache key is a combination of the view name and the current slugified url. That way this can be used with multisite setups:
include-cache::site-{ Str::slug(url('/')) }-{ $viewName }'
Usage
@includeCached('view.name')
Notes
Keep in mind that any dynamic things within the view will not be executed when cached. For example @push, see Blade Stacks. Also Blade Icons Deferring doesn't work, you have to use these things outside the cached include!
Helpers
optionalDeep
Have you heard of optional()? This is the supercharged version working at any depth! It makes sure that any missing key will not break your code, especially helpful when mixing Statamic with Blade
Usage
It will automatically return the value when casting to string so you can immediately echo out it's value, if you want to get the value use the get method. This will return null if anywhere along the chain the value or key does not exist.
{{ optionalDeep($object)->undefinedKey->anotherUndefinedKey }} {{ optionalDeep($object)->header->usp->link->value() }} @if(optionalDeep($object)->header->usp->link->value()->isset()) @if(optionalDeep($object)->header->usp->link->value()->get() === 'test')
Tip
the OptionalDeep class implements Macroable, allowing you to extend it with your own functions!
License
GNU General Public License v3. Please see License File for more information.
rapidez/blade-directives 适用场景与选型建议
rapidez/blade-directives 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 113.42k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 06 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rapidez」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rapidez/blade-directives 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rapidez/blade-directives 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rapidez/blade-directives 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Rapidez Reviews
Rapidez Experius Postcode NL implementation
Rapidez Amasty Label
Rapidez MSI
Rapidez MagePrince Faq
Implements VIES validation to use on VAT fields in Rapidez
统计信息
- 总下载量: 113.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 15
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2023-06-28