ahurov/string-blade-compiler

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

4.1.0 2020-05-19 07:14 UTC

README

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": {
	...
	"ahurov/string-blade-compiler": "VERSION"
},

获取版本,例如 'composer show ahurov/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/ahurov/string-blade-compiler

composer require "ahurov/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 服务提供者,已发送到 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。

我似乎找不到这个设置实际上何时被使用。如果你需要这个功能,请提交一个错误报告,我会看看是否可以添加这个功能。

// 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 许可证。