leitsch / kirby-blade
为 Kirby 4 启用 Laravel Blade 模板引擎
Requires
- php: ^8.2
- getkirby/cms: ^4.0
- getkirby/composer-installer: ^1.2
- illuminate/config: ^11.0
- illuminate/view: ^11.0
Requires (Dev)
- laravel/pint: ^1.14
This package is auto-updated.
Last update: 2024-09-05 06:37:43 UTC
README
Kirby Blade 使用 Laravel illuminate/view
11.x 包,并兼容 Kirby 4。
此包为您的 Kirby 应用程序启用 Laravel Blade。
安装
composer require leitsch/kirby-blade
注意: Laravel 和 Kirby 都定义了 e()
辅助函数,但它们执行的任务大不相同。在 Kirby 中,e()
主要是 echo $condition ? $a : $b;
的快捷方式。在 Laravel 中,此函数会转义字符串中的 HTML 字符。从 Kirby 3.7 版本开始,您必须在包含 autoload.php
文件之前,在 index.php
中添加一行代码来禁用 Kirby 的自带的 e()
辅助函数。
define('KIRBY_HELPER_E', false);
什么是 Blade?
根据 Laravel Blade 文档,Blade 是
Blade 是 Laravel 内置的简单而强大的模板引擎。与某些 PHP 模板引擎不同,Blade 允许您在模板中使用纯 PHP 代码。实际上,所有 Blade 模板都编译成纯 PHP 代码,并在修改之前缓存,这意味着 Blade 对您的应用程序几乎没有开销。Blade 模板文件使用 .blade.php 文件扩展名。
使用方法
您可以使用 Blade 的功能,如 布局、表单、子视图、组件、指令 和您自定义的 if 语句。
有关 Laravel Blade 的所有文档都在 官方文档 中。
选项
此包的默认值为
所有值都可以在 config.php
文件中更新。
模板
默认模板文件夹是 site/templates
目录或您定义的 templates
目录,但您可以轻松地更改它
'leitsch.blade.templates' => '/theme/default/templates',
视图
所有生成的视图都存储在 site/cache/views
目录或您定义的 cache
目录,但您可以轻松地更改它
'leitsch.blade.views' => '/site/storage/views',
指令
默认情况下,Kirby Blade 包含以下指令
@asset($path) @csrf() @css($path) @dump($variable) @e($condition, $value, $alternative) @get($key, $default) @gist($url) @go($url, $code) @h($string, $keepTags) @html($string, $keepTags) @js($path) @image($path, $attr) // @image('forrest.jpg', 'url') @kirbytag($type, $value, $attr) @kirbytags($text, $data) @kirbytext($text, $data) @kirbytextinline($text) @kt($text) @markdown($text) @option($key, $default) @page($key, $attr) // @page('blog', 'title') @param($key, $fallback) @site($attr) // @site(title') @size($value) @smartypants($text) @snippet($name, $data) @svg($file) @t($key, $fallback) @tc($key, $count) @tt($key, $fallback, $replace, $locale) @u($path, $options) @url($path, $options) @video($url, $options, $attr) @vimeo($url, $options, $attr) @widont($string) @youtube($url, $options, $attr)
但您可以创建自己的
'leitsch.blade.directives' => [ 'greeting' => function ($text) { return "<?php echo 'Hello: ' . $text ?>"; }, ],
Kirby 辅助函数文档
https://getkirby.com/docs/reference/templates/helpers
if 语句
像指令一样,您可以创建自己的 if 语句
'leitsch.blade.ifs' => [ 'logged' => function () { return !!kirby()->user(); }, ],
声明后,您可以像这样使用它
@logged Welcome back {{ $kirby->user()->name() }} @else Please Log In @endlogged
匿名组件
要定义一个匿名组件,您只需要在您的 site/templates/components
目录中放置一个 Blade 模板。要渲染警报组件,您必须定义 site/templates/components/alert.blade.php
,组件可以像这样渲染
<x-alert />
有关匿名组件的更多信息,请参阅 官方 Laravel Blade 文档。
基于类的组件
对于基于类的组件,必须将 app 命名空间添加到您的项目的 composer.json
文件中。
{ "autoload": { "psr-4": { "App\\": "app/" } } }
该类必须放在 site/components
目录中,并由包自动加载。
按钮类的示例可能如下所示
<?php namespace App\View\Components; use Illuminate\Support\Facades\View; use Illuminate\View\Component; class Button extends Component { public function render() { return View::make('components.button'); } }
按钮类的blade文件应该放在site/templates/components/button.blade.php
目录下。
钩子
对于HTML压缩等使用场景,有一个自定义钩子用于操作渲染后的HTML输出
# site/config/config.php # For this example, we are using 'voku/html-min' use voku\helper\HtmlMin; return [ # ... 'hooks' => [ 'blade.render:after' => function (string $html): string { return (new HtmlMin())->minify($html); }, ], # ... ];