philip1337 / string-blade-compiler
Laravel Blade 字符串编译器,将字符串渲染为 Blade 模板
Requires
- php: ^7.2
- laravel/framework: ^8.0
- 4.2.x-dev
- 4.2.0
- 4.1.x-dev
- 4.1
- 4.0.x-dev
- 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-4.0m
- dev-revert-46-laravel-5.8
- dev-TerrePorter-patch-1
- dev-master
This package is auto-updated.
Last update: 2023-12-23 19:47:06 UTC
README
从字符串值渲染 Blade 模板。
重写的版本,允许将数组传递给视图函数,而不是模板文件名。
这是 \Illuminate\View\View 的直接扩展,旨在替换其使用。它将替换默认的 View 实例。
版本
String Blade | Laravel 版本 |
---|---|
4.2 | Laravel 8 |
4.1 | Laravel 7 |
4.0 | Laravel 6 |
3.8 | Laravel 5.8 |
3.7 | Laravel 5.7 |
3.6 | Laravel 5.6 |
3.5 | Laravel 5.5 |
3.4 | Laravel 5.4 |
3.3 | Laravel 5.2 |
3.2 | Laravel 5.1 |
2.* | Laravel 5 |
1.* | Laravel 4.2 |
版本 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": {
...
"philip1337/string-blade-compiler": "VERSION"
},
获取版本 'composer show philip1337/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/philip1337/string-blade-compiler
composer require "philip1337/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的使用位置。如果是您在编译后的视图文件中进行挖掘,这将允许您看到StingBladeCompiler文件的标签。
配置选项
// 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+)。设置正在使用的编译器的删除标志,字符串blade或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许可证授权