enbit/string-blade-compiler

Laravel Blade 字符串编译器,将字符串作为 Blade 模板渲染

4.0.1 2020-06-03 21:42 UTC

README

String Blade Compiler

Laravel 6 License

从字符串值渲染 Blade 模板。

重写版本,允许将数组传递给视图函数而不是模板文件名。

这是对 \Illuminate\View\View 的直接扩展,旨在替换其使用。它将替换默认的 View 实例。

版本

版本 3.8 : 更新

该包已被完全重写,所有代码均已更新,以更符合 Laravel 版本代码。扩展类中的一些函数不再需要,已被删除,代码也进行了整理。

还更新了测试,使其与 Laravel View Tests 中可用的内容一致。其中一些不适用于 StringBlade。

变更,

  • 现在使用 Laravel 自动注册功能。
  • 不再需要从 config/app.php 中删除 Illuminate\View\ViewServiceProvider::class
  • 从 Laravel 中移除了 setRawTagssetContentTagssetEscapedTags 的能力(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 在自动加载之前加载的拉取请求已发送到 Laravel/Framework 并被拒绝。

配置

编译后的字符串模板的默认缓存时间为 300 秒(5 分钟),这可以在配置文件中更改或在调用视图时更改。更改适用于所有字符串模板。

注意:如果您使用 homestead 或其他 vm,则主机处理缓存文件的 filemtime。这意味着 vm 可能与文件有不同的时间。如果缓存未按预期过期,请检查系统之间的时间。

用法

本包提供了一个与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

我似乎找不到何时实际使用了该设置。如果你想使用这个功能,请提交一个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许可证授权