mitinsany/string-blade-compiler

此包已废弃,不再维护。未建议替代包。

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

5.7.x-dev 2019-02-14 18:02 UTC

This package is auto-updated.

Last update: 2024-08-15 06:31:07 UTC


README

Laravel 5.7 License

从字符串值渲染 Blade 模板。

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

这是 \Illuminate\View\View 的直接扩展,并替换了默认版本。

版本

此版本 1 是为 Laravel 4.2,版本 2 是为 Laravel 5。

版本 3 是完全重写,用于 Laravel 5.1

版本 3.2 是用于 Laravel 5.2 的版本。

版本 3.3 是用于 Laravel 5.3 的版本。

版本 3.4 是用于 Laravel 5.4 的版本。

版本 3.5 是用于 Laravel 5.5 的版本。

版本 3.6 是用于 Laravel 5.6 的版本。

版本 3.7 是用于 Laravel 5.7 的版本。

安装

将包添加到 composer.json

"require": {
	...
	"wpb/string-blade-compiler": "VERSION"
},

在 packagist.org 上 https://packagist.org.cn/packages/wpb/string-blade-compiler

或从控制台使用 require: composer require "wpb/string-blade-compiler"

要获取版本,使用 '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'

In config\app.php, providers section

将 'Illuminate\View\ViewServiceProvider::class' 替换为 'Wpb\String_Blade_Compiler\ViewServiceProvider::class',

无需向别名数组中添加 Facade,因为服务提供者在包的 ServiceProvider 中自动包含。

配置

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

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

注意:请参阅以下新选项以在渲染后删除视图缓存(适用于 stringblade 和 blade 编译器)。

用法

此包提供了一个与 View 语法相同的 StringView Facade,但接受 Array 或 Array Object 实例而不是视图路径。

此版本已移除 'updated_at' 数组值,新增了一个名为 secondsTemplateCacheExpires 的新选项。

这是自模板编译文件上次修改以来的秒数,例如 'time() >= ($this->files->lastModified($compiled) + $viewData->secondsTemplateCacheExpires)'

如果设置了 cache_key,则将使用它作为编译键或使用 md5(template)。

// existing file template load
return view ('bladetemplatefile', ['token' => 'I am the child template']);

// string template load
return view (['template' => '{{$token}}'], ['token' => 'I am the child template']);

// 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
			),
			array(
				'token1'=> 'token 1 value'
			)
	);

还允许 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);	

更改标签

  // change the tags
    StringBlade::setRawTags('[!!', '!!]',escapeFlag);
    StringBlade::setContentTags('[[', ']]',escapeFlag);
    StringBlade::setEscapedContentTags('[[[', ']]]',escapeFlag);

'escapeFlag',如果为 true 则标签将被转义,如果为 false 则不会转义(与 setContentTagsEscaped 函数相同)

删除生成的编译缓存视图文件(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 许可证授权