neohq / string-blade-compiler
Laravel Blade 字符串编译器,将字符串渲染为 blade 模板
Requires
- php: ^7.1.3
- laravel/framework: ^6.0
This package is not auto-updated.
Last update: 2024-09-27 08:09:32 UTC
README
从字符串值渲染 blade 模板。
重构版本,允许将数组传递给视图函数,而不是模板文件名。
这是 \Illuminate\View\View 的直接扩展,旨在替换其使用。它将替换默认的 View 实例。
版本
版本 3.8.2 : 更新
更新代码以支持 Laravel 6.0
版本 3.8 : 更新
该包已完全重写,所有代码更新以更符合 Laravel 版本代码。扩展类中的几个函数不再需要,已删除,代码已进行了一些清理。
还更新了测试,使其与 Laravel View 测试中可用的测试一致。其中一些不适用于 StringBlade。
更改:
- 现在使用 Laravel 自动注册功能。
- 不再需要从
config/app.php
中移除Illuminate\View\ViewServiceProvider::class
。 setRawTags
、setContentTags
和setEscapedTags
的能力已从 Laravel 中删除(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 分钟),这可以在配置文件或调用视图时更改。更改适用于所有字符串模板。
注意:如果使用 homestead 或其他虚拟机,宿主处理缓存文件的 filemtime。这意味着虚拟机的时间可能与文件不同。如果缓存没有按预期到期,请检查系统之间的时间。
用法
此包提供了一个与 View 语法相同的 StringView 门面,但接受 Array 或 Array Object 实例,而不是视图路径。
新配置选项
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。
我似乎找不到这个设置实际被使用的情况。如果你需要这个功能,请提交一个错误报告,我会考虑添加这个功能。
// 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 许可证。