brunocfalcao/laravel-blade-helper

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

dev-master 2024-03-17 08:58 UTC

This package is auto-updated.

Last update: 2024-09-06 22:45:32 UTC


README

Latest Version on Packagist Build Status CI Status Total Downloads License

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

在创建新的自定义 Blade 指示符时,使用 Blade::directive(…) 方法,唯一可以操作的参数是从 .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 对象以 blade.helper 的名称绑定到 Laravel 的服务容器中,可以通过解析该名称使用。为了方便起见,还提供了一个 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)。请参阅许可文件以获取更多信息。