arrilot / bitrix-blade
此包已被弃用且不再维护。未建议替代包。
0.8.0
2020-10-13 16:05 UTC
Requires
- php: >=5.4.0
- arrilot/bitrix-hermitage: ^1
- illuminate/container: 5.*
- illuminate/events: 5.*
- illuminate/view: 5.*
README
此包不再活跃支持
原因 - 我们不再使用Bitrix在我们的项目中。如果您对此项目感兴趣并希望参与维护 - 分支并在此存储库中创建Issue,我们将在此处放置分支链接。
分支
Bitrix Blade - 在Bitrix中整合Blade模板引擎
安装
1)composer require arrilot/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 - 如果在该目录中找不到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();?>
检查,它会在编译视图时自动添加。同时,也会执行extract($arResult, EXTR_SKIP);
- 为了使语言文件从模板中加载,需要将其命名为
template.php
附加信息
PhpStorm
- 要在 PhpStorm 中启用 .blade 文件的语法高亮,需要在
Settings->Editor->File Types->Blade
中添加此扩展。 - 为了使 PhpStorm 能够正确识别和突出显示此包中的自定义指令,可以将它们添加到其中。这可以通过
Settings->Language & Frameworks->PHP->Blade->Directives
完成。