midwestern-interactive / blade-directive-helpers
Blade模板中常用任务加速的Blade指令助手集合
Requires
- php: ^7.0
This package is not auto-updated.
Last update: 2023-09-30 22:49:11 UTC
README
Blade模板中常用任务加速的Blade指令集合
安装
- 将包含的两个
php
文件移动到/app/Providers/
。 - 在
/config/app.php
中添加
/** * Custom Service Providers */ App\Providers\BladeDirectiveProvider::class, App\Providers\BladeIfDirectiveProvider::class,
- 上面的操作将启用你的Blade模板中的指令。
由于Blade标签将在模板内编译成普通的PHP,你必需通过php artisan view:clear
调用清除编译后的视图!
用法
Blade助手指令
**Blade指令是一个生成标记的简短语法。
asset
**@asset
指令通过追加文件修改时间戳的Unix时间戳值来提供缓存破坏功能。
<link href="@asset('/css/app.css')" rel="stylesheet">
上面的标记会产生...
<link href="/css/app.css?v=1506526109" rel="stylesheet">
这有效地消除了不想要的缓存,因为每次部署app.css
时,文件修改时间戳都会更新,从而相应地改变CSS文件的URL中的v=
值。
上面的示例是一个CSS示例,但你可以为任何你想启用缓存破坏的本地文件使用它。
icon
@icon
指令允许你生成简单图标的完整语法(更复杂的图标,例如分层、旋转等,仍需要手动编写)。假设字体库为Font-awesome。
在其最简单的形式中,你只需传递图标的名称。
<a href="#/">@icon('envelope')Messages</a> <!-- Produces --> <!-- <a href="#"><i class="fa fa-envelope"></i></a> -->
你还可以传递两个附加参数。第二个参数是$provider
,例如glyphicon。这应该是添加到元素的类。第三个参数你可以使用标签类型(i
、span
等)。**如果你需要访问第三个标签,你必须传递第二个**!
此示例的完整示例为...
<a href="#">@icon('envelope', 'glyphicon', 'span') Messages</a> <!-- Produces --> <!-- <a href="#"><span class="glyphicon glyphicon-envelope"></span></a> -->
Blade If指令
Blade "If"指令允许你简写通常需要手动编写的if
语句。这些内在地有一个可用的else
块。
hasPermission
has permission指令允许你包裹你只想在用户具有给定权限时显示的标记。可以用@can
指令替换。
@hasPermission('manage-users') <li><a href="...">Manage Users</a></li> @else <!-- Alternate link if they can't manage users --> @endhasPermission ...
isInRole
检查用户是否在给定角色中
... @isInRole('admin') <li><a href="...">Manage Users</a></li> @else <!-- Alternate link if they can't manage users --> @endisInRole ...
isActive
这用于检查链接是否当前是活动链接
<a href="#" @isActive('/')class="active"@endisActive>Home</a>
此指令内部调用Request::is(...)
函数,因此你可以传递任何可以用于那里的字符串。例如,@isActive('users*)
将匹配/users
以及/users/manage/1
与所有if指令一样,有一个可用的else
块。例如,<a href="/users" class="@isActive('users')active @else muted@endisActive>Users</a>