dfox288/string-blade-compiler

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

8.0.1 2023-03-01 11:17 UTC

This package is not auto-updated.

Last update: 2024-09-26 17:28:46 UTC


README

Laravel 8 License

从字符串值渲染 Blade 模板。

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

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

版本

版本 3.8 : 更新

该包已被完全重写,所有代码都已更新,使其更符合 Laravel 版本代码。扩展类中的几个函数不再需要,已被删除,并且代码已进行了一些清理。

还更新了测试,以符合 Laravel View 测试中的内容。其中一些不适用于 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 的 Service Providers 预加载过程中存在问题。使用自动加载注册的 Service Providers 在设置在 /config/app.php 中的 Service Providers 之前实例化。这可能会导致 StringBladeCompiler 早期版本出现问题。该版本已重新编写以考虑自动加载过程。

一个将 config/app.php 文件中注册的供应商服务加载在自动加载之前加载的 pull request 已发送到 Laravel/Framework,但被拒绝了。

配置

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

STRING_BLADE_CACHE_TIMEOUT=300

可以在配置文件和环境文件中更改 blade 自定义指令的自动加载。

STRING_BLADE_AUTOLOAD=false

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

使用

本包提供了一个StringView界面,其语法与View相同,但接受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许可证授权。