strong / bitrix-blade
0.8.2
2023-04-15 20:19 UTC
Requires
- php: >=8.1
- illuminate/container: 8.*
- illuminate/events: 8.*
- illuminate/view: 8.*
Requires (Dev)
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-15 23:27:46 UTC
README
Bitrix Blade - 将Blade模板引擎集成到Bitrix中
安装
1)composer require strong/bitrix-blade
- 添加到init.php
use Arrilot\BitrixBlade\BladeProvider; require $_SERVER['DOCUMENT_ROOT']."/vendor/autoload.php"; BladeProvider::register();
使用
将组件模板从template.php
替换为template.blade
,然后可以使用Blade
指令@include('path.to.view')
的修改如下
- 首先在当前模板组件目录(template.blade所在的位置)中查找view
- 如果在该目录中没有找到,则相对于基本目录(默认为
local/views
,但可以在调用BladeProvider::register()
时指定其他路径)查找
自定义指令(custom directives)
为了添加自定义指令,需要在编译器中注册它
$compiler = BladeProvider::getCompiler();
$compiler->directive('directiveName', function ($expression) {
return '...';
});
在安装包时,BladeProvider::register()
已经为您自动注册了一些有用的指令
@bxComponent
- 等同于$APPLICATION->IncludeComponent()
@block('key')
和@endblock
- 之间的内容将在调用$APPLICATION->ShowViewContent('key')
的位置输出@lang('key')
- 等同于{!! Bitrix\Main\Localization\Loc::getMessage('key') !!}
@auth
和@endauth
- 等同于<?php if($USER->IsAuthorized()) ?> ... <?php endif ?>
@guest
和@endguest
- 类似,但检查未授权用户。@admin
和@endadmin
- 类似,但使用$USER->IsAdmin()
@csrf
- 等同于<input type="hidden" name="sessid" value="{!! bitrix_sessid() !!}" />
- 与缓存相关的指令
配置
如有必要,可以在配置文件.settings_extra.php
中更改路径。
'bitrix-blade' => [ 'value' => [ 'baseViewPath' => '/absolute/path/or/path/from/document/root', // по умолчанию 'local/views' 'cachePath' => '/absolute/path/or/path/from/document/root', // по умолчанию 'local/cache/blade' ], 'readonly' => false, ],
清除缓存
为了确保Blade的高效运行,它会将编译后的模板缓存为PHP文件。在大多数情况下,不需要手动清理此缓存,因为Blade会自动比较模板文件和缓存的修改时间,并自动使缓存失效。然而,在某些情况下(例如添加新的自定义指令时),仍然需要手动清除缓存。这可以通过调用BladeProvider::clearCache()
方法来完成。
一些注意事项
- Bitrix仅允许在组件模板中使用第三方模板引擎,网站模板仅支持PHP。
- 由于明显的原因,无法充分利用模板继承。
- 不能使用传统的扩展名
.blade.php
。Bitrix看到.php
就会启动PHP引擎。 - 在模板中应使用
$template
代替$this
- 例如$template->setFrameMode(true);
- 不需要在blade模板中编写
<?if(!defined("B_PROLOG_INCLUDED")||B_PROLOG_INCLUDED!==true) die();?>
检查,它会自动添加到编译后的view中。同时,还会执行extract($arResult, EXTR_SKIP);
- 为了使语言文件从模板中加载,其名称应与通常一样 -
template.php
附加信息
PhpStorm
- 为了在PhpStorm中为.blade文件启用语法高亮,需要在
Settings->Editor->File Types->Blade
中添加此扩展 - 为了使PhpStorm正确理解和突出显示此包中的自定义指令,可以将其添加到其中。这可以通过在
Settings->Language & Frameworks->PHP->Blade->Directives
中完成。