lexdubyna / blade
Laravel 的 Blade 模板引擎的独立版本,可在 Laravel 之外使用。
v1.5.0
2022-12-08 11:24 UTC
Requires
- php: >=7.0
- illuminate/view: ^5.5|^6.0|^7.0|^8.0
Requires (Dev)
- phpunit/phpunit: ^6.0|^7.0
- satooshi/php-coveralls: ^1.0
This package is not auto-updated.
Last update: 2024-09-27 17:36:57 UTC
README
用于 Laravel 之外的 Laravel 的 Blade 模板引擎的独立版本。
安装
使用 composer 安装
composer require lexdubyna/blade
用法
通过传递视图文件所在文件夹和缓存文件夹创建 Blade 实例。通过调用 make
方法渲染模板。有关 Blade 模板引擎的更多信息,请参阅 https://laravel.net.cn/docs/8.x/blade。
use Lexdubyna\Blade\Blade; $blade = new Blade('views', 'cache'); echo $blade->make('homepage', ['name' => 'John Doe'])->render();
或者,您还可以使用简写方法 render
echo $blade->render('homepage', ['name' => 'John Doe']);
您还可以通过 directive()
函数扩展 Blade
$blade->directive('datetime', function ($expression) { return "<?php echo with({$expression})->format('F d, Y g:i a'); ?>"; });
这允许您在 blade 模板中使用以下内容
Current date: @datetime($date)
Blade 实例将所有方法传递给内部视图工厂。因此,如 exists
、file
、share
、composer
和 creator
等方法也是可用的。有关更多信息,请参阅 原始文档。
组件
您可以使用此包使用 视图组件。
要使用基于类和匿名组件,您需要注册它们
$blade->compiler()->components([ 'alert' => App\View\Components\Alert::class, // <x-alert type="success" message="OK" /> 'components.anonymous.link' => 'link' // <x-link /> ])
基于类的组件
您的类组件必须扩展 Jenssegers\Blade\ViewComponent
并具有受保护的属性 $template
namespace App\View\Components; use Lexdubyna\Blade\ViewComponent; class Alert extends ViewComponent { // all the public properties will be exposed inside the template public string $type; public string $message; protected string $template = 'components.alert'; // $template is required, it's a path to a blade template file public function __construct($type, $message) { $this->type = $type; $this->message = $message; } }
待办事项
- 组件测试
- 使其与
illuminate/view^9.0
兼容