wpb / string-blade-compiler
Laravel Blade 字符串编译器,将字符串渲染为 Blade 模板
Requires
- php: ^7.2|^8.0
- laravel/framework: ^6.0|^7.0|^8.0
- 7.0.x-dev
- 6.0.x-dev
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.0.x-dev
- 5.0.2
- 5.0.1
- v5.0.0
- 4.0.0
- 3.8.x-dev
- 3.8.1
- 3.8.0
- 3.7.x-dev
- 3.7.0
- 3.6.x-dev
- 3.6
- 3.5.x-dev
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.4.x-dev
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.x-dev
- 3.3.0
- 3.2.x-dev
- 3.2.2
- 3.2.1
- 3.2.0
- 3.0.x-dev
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.0
- 2.0.x-dev
- 2.0.0
- 1.0.x-dev
- 1.0.0
- dev-revert-59-laravel-7-and-autoload-blade-custom-directives
- dev-revert-46-laravel-5.8
- dev-TerrePorter-patch-1
- dev-master
This package is auto-updated.
Last update: 2024-08-29 13:32:13 UTC
README
从字符串值渲染 Blade 模板。
重构版本,允许传递数组到视图函数而不是模板文件名。
这是 \Illuminate\View\View 的直接扩展,旨在替换其使用。它将替换默认的 View 实例。
版本
版本 3.8 : 更新
该包已完全重写,所有代码都已更新以更好地与 Laravel 版本代码保持一致。扩展类中的几个函数不再需要,已被删除,代码已稍微清理。
还更新了测试,使其与 Laravel View Tests 中可用的内容一致。其中一些不适用于 StringBlade。
更改:
- 现在使用 Laravel 自动注册功能。
- 不再需要从 config/app.php 中移除
Illuminate\View\ViewServiceProvider::class
。 - 从 Laravel 中移除了
setRawTags
、setContentTags
和setEscapedTags
的功能(laravel/framework#17736)。它们已过时。 - 由于我没有找到实际使用该功能的代码,编译器的
setDeleteViewCacheAfterRender
函数已被弃用。 - 添加了更多测试
安装
将包添加到 composer.json
"require": {
...
"wpb/string-blade-compiler": "VERSION"
},
获取版本 'composer show wpb/string-blade-compiler',例如 'dev-master, * 3.2.x-dev, 3.2.0, 3.0.x-dev, 3.0.0, 2.1.0, 2.0.x-dev, 2.0.0, 1.0.x-dev, 1.0.0'
在 packagist.org 上 https://packagist.org.cn/packages/wpb/string-blade-compiler
composer require "wpb/string-blade-compiler"
配置
在 config\app.php 的 providers 部分
ServiceProvider 和 Facade Alias 都由 Laravel 自动注册。无需将它们添加到 /config/app.php 文件中。
将 'Illuminate\View\ViewServiceProvider::class' 替换为 'Wpb\String_Blade_Compiler\ViewServiceProvider::class',
此版本不需要您删除原始视图组件的注册。在 ServiceProvider 注册时,它将用其自身替换视图绑定。
Laravel 的自动加载器
目前 Laravel ServiceProviders 的预载过程中存在一个问题。使用自动加载注册的 ServiceProviders 在设置在 /config/app.php 中的 ServiceProviders 之前实例化。这可能导致 StringBladeCompiler 的早期版本出现问题。该版本已重写以考虑自动加载过程。
一个将注册在
config/app.php
文件中的 vendor ServiceProviders 在自动加载之前加载的 pull request 已发送到 Laravel/Framework 并被拒绝。
- 如果您需要在此或任何其他包在 vendor 自动加载之前加载,请这样做 - https://gist.github.com/TerrePorter/4d2ed616c6ebeb371775347f7378c035
配置
编译的字符串模板的默认缓存时间为 300 秒(5 分钟),这可以在配置文件、环境文件或调用视图时更改。更改适用于所有字符串模板。
STRING_BLADE_CACHE_TIMEOUT=300
在配置文件和环境文件中可以更改 blade 自定义指令的自动加载。
STRING_BLADE_AUTOLOAD=false
注意:如果您使用 homestead 或其他虚拟机,则主机处理缓存文件的 filemtime。这意味着虚拟机可能的时间与文件不同。如果缓存没有按预期过期,请检查系统之间的时间。
使用方法
此包提供了一个与View语法相同的StringView界面,但它接受一个数组或数组对象实例,而不是视图路径。
新配置选项
Laravel 5.8 BladeCompiler在编译后的模板文件中添加了一个PHP注释php $contents .= "<?php /**PATH {$this->getPath()} ENDPATH**/ ?>";
。由于StringBladeCompiler没有“路径”即“模板文件位置”这一对开发者有用的属性。我添加了一个新的配置值,templateRefKey
。这允许开发者标记StringBladeCompiler的使用位置。这样,当你深入查看编译后的视图文件时,你可以看到StringBladeCompiler文件的标记。
配置选项
// existing file template load (the original View() method return view ('bladetemplatefile',['token' => 'I am the token value']);
// string blade template load return view (['template' => '{{$token}}'], ['token' => 'I am the token value']);
// you can mix the view types $preset = view (['template' => '{{$token}}'], ['token' => 'I am the token value']); return view ('bladetemplatefile', ['token' => $preset]);
// full list of options return view( array( // this actual blade template 'template' => '{{ $token1 }}', // this is the cache file key, converted to md5 'cache_key' => 'my_unique_cache_key', // number of seconds needed in order to recompile, 0 is always recompile 'secondsTemplateCacheExpires' => 1391973007, // sets the PATH comment value in the compiled file 'templateRefKey' => 'IndexController: Build function' ), array( 'token1'=> 'token 1 value' ) );
由于StringBlade是原始View的扩展类,你应该可以使用StringBlade做任何你通常用View做的事情。
Blade::extend,例如
由于编译器被设置为独立的实例,如果你需要在字符串和文件模板上扩展(或指令),你需要将扩展(或指令)附加到两个编译器上。
// allows for @continue and @break in foreach in blade templates StringBlade::extend(function($value) { return preg_replace('/(\s*)@(break|continue)(\s*)/', '$1<?php $2; ?>$3', $value); }); Blade::extend(function($value) { return preg_replace('/(\s*)@(break|continue)(\s*)/', '$1<?php $2; ?>$3', $value); });
其他选项
// change the contente tags escaped or not StringBlade::setContentTagsEscaped(true); // for devel force templates to be rebuilt, ignores secondsTemplateCacheExpires StringBlade::setForceTemplateRecompile(true);
// change the contente tags escaped or not Blade::setContentTagsEscaped(true); // for devel force templates to be rebuilt, ignores secondsTemplateCacheExpires Blade::setForceTemplateRecompile(true);
// @deprecated This feature was removed from Laravel (https://github.com/laravel/framework/issues/17736) // change the tags StringBlade::setRawTags('[!!', '!!]',escapeFlag); // default {!! !!} StringBlade::setContentTags('[[', ']]',escapeFlag); // default {{ }} StringBlade::setEscapedTags('[[[', ']]]',escapeFlag); // default {{{ }}} __ Functions are still there, use at your own risk. __
删除生成的编译缓存视图文件(v3+)。设置正在使用的编译器(stringblade或blade)的删除标志。
我似乎找不到这个设置何时被实际使用。如果你想这样做,提交一个bug,我会看看是否添加这个功能。
// set flag to delete compiled view cache files after rendering for stringblade compiler View::getEngineFromStringKey('stringblade')->setDeleteViewCacheAfterRender(true); // set flag to delete compiled view cache files after rendering for blade compiler View::getEngineFromStringKey('blade')->setDeleteViewCacheAfterRender(true);
许可证
string-blade-compiler是开源软件,采用MIT许可证。