imliam/laravel-blade-helper

定义自定义 Blade 指示符的更简单方式。

资助包维护!
imliam

安装: 112,288

依赖者: 2

建议者: 0

安全: 0

星星: 178

关注者: 3

分支: 15

v1.5.0 2023-04-25 23:41 UTC

README

Latest Version on Packagist Build Status CI Status Total Downloads License

定义自定义 Blade 指示符的更简单方式。

当使用 Blade::directive(…) 方法创建新的自定义 Blade 指示符时,唯一可用于操作的参数是从 .blade.php 文件传递的原始字符串表达式。开发者在指示符内解析表达式的内容似乎很少见,相反,他们选择将整个表达式作为参数传递给辅助函数或另一个类的方法。例如

Illuminate\Support\Facades\Blade::directive('uppercase', function($expression) {
    return "<?php echo strtoupper($expression); ?>";
});

由于这似乎是最常见的用例,这个包试图帮助定义这些辅助函数,使其定义更加容易,而不需要返回字符串或考虑在创建指示符时表达式的含义。

💾 安装

您可以使用以下命令使用 Composer 安装此包

composer require imliam/laravel-blade-helper:^1.0

📝 使用方法

BladeHelper 对象绑定到 Laravel 服务容器的名称为 blade.helper,可以通过解析它来使用。同时提供 Facade 以便方便使用。要定义辅助函数,请使用 directive(…) 方法

app('blade.helper')->directive(…);

\ImLiam\BladeHelper\Facades\BladeHelper::directive(…);

此方法接受两个参数;第一个是指示符的名称,第二个是指示符应该调用的函数

// Define the helper directive
BladeHelper::directive('uppercase', 'strtoupper');

// Use it in a view
@uppercase('Hello world.')

// Get the compiled result
<?php echo strtoupper('Hello world.'); ?>

// See what's echoed
"HELLO WORLD."

如果没有提供第二个参数,指示符将尝试调用同名的函数

// Define the helper directive
BladeHelper::directive('join');

// Use it in a view
@join('|', ['Hello', 'world'])

// Get the compiled result
<?php echo join('|', ['Hello', 'world']); ?>

// See what's echoed
"Hello|world"

第二个参数也可以是一个回调。与 Laravel 提供的典型 Blade::directive(…) 方法相比,这里回调的优势是可以定义回调的特定参数,而不仅仅是获取作为字符串的原始表达式。这为创建 Blade 辅助指示符的过程带来了几个优势

  • 为回调的参数添加类型提示
  • 在指示符调用时,可以使用单个参数进行操作,而不是作为字符串的原始表达式
  • 定义指示符而无需将其仅作为应用程序其他部分的辅助函数或类的代理使用
// Define the helper directive
BladeHelper::directive('example', function($a, $b, $c = 'give', $d = 'you') {
    return "$a $b $c $d up";
});

// Use it in a view
@example('Never', 'gonna')

// Get the compiled result
<?php echo app('blade.helper')->getDirective('example', 'Never', 'gonna'); ?>

// See what's echoed
"Never gonna give you up"

默认情况下,所有辅助指示符在使用时都会将其内容输出到视图。可以通过将 false 作为第三个参数传递来禁用此功能

// Define the helper directive
BladeHelper::directive('log', null, false);

// Use it in a view
@log('View loaded…')

// Get the compiled result
<?php log('View loaded…'); ?>

// Nothing is echoed

示例辅助指示符

自定义 Blade 辅助的一个示例是包装 FontAwesome 4 图标,以便更方便地添加交替文本以提高可访问性

// Define the helper directive
BladeHelper::directive('fa', function(string $iconName, string $text = null, $classes = '') {
    if (is_array($classes)) {
        $classes = join(' ', $classes);
    }

    $text = $text ?? $iconName;

    return "<i class='fa fa-{$iconName} {$classes}' aria-hidden='true' title='{$text}'></i><span class='sr-only'>{$text}</span>";
});

// Use it in a view
@fa('email', 'Envelope')

自定义 "if" 指示符

Laravel Blade 提供了一种方便的方法来定义自定义 "if" 语句指示符。Blade Helper 包提供了生成这些指示符的附加方法,包括自动生成 ifelseifendif 变体。

可以像定义指示符方法一样定义 if 语句,但必须将其第二个参数作为可调用对象

BladeHelper::if('largestFirst', function(int $a, int $b): bool {
    return $a > $b;
});

定义后,可以直接在 Blade 模板中使用辅助函数

@largestFirst(1, 2)
    Lorem ipsum
@elseLargestFirst(5, 3)
    dolor sit amet
@else
    consectetur adipiscing elit
@endLargestFirst

✅ 测试

composer test

🔖 变更日志

请参阅变更日志文件获取有关最近更改的更多信息。

⬆️ 升级

请参阅升级文件以获取有关从先前版本升级的详细信息。

🎉 贡献

请参阅贡献文件行为准则以获取有关为项目贡献的详细信息。

🔒 安全性

如果您发现任何与安全相关的问题,请通过电子邮件liam@liamhammett.com联系,而不是使用问题跟踪器。

👷 贡献者

♻️ 许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件