承接 wpb/string-blade-compiler 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wpb/string-blade-compiler

Composer 安装命令:

composer require wpb/string-blade-compiler

包简介

Laravel Blade String Compiler, render string as blade templates

README 文档

README

Laravel 8 License

Render Blade templates from string value.

Reworked version to allow for array to be passed to the view function instead of template file name.

This is a direct extension of \Illuminate\View\View and is build to replace its usage. It will replace the default View instance.

Versions

String Blade Laravel Version
6.0 Laravel 8
5.0 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

Version 3.8 : Updates

The package has been completely rewritten, all code updated to be more in line with the Laravel version code. Several of the functions in the extended class were not needed and have been removed and the code has been cleaned up a bit.

Also updated the tests to what is available in Laravel View Tests. Some are not applicable to StringBlade.

Changes,

  • Now uses Laravel auto registration feature.
  • No long need to remove Illuminate\View\ViewServiceProvider::class from config/app.php
  • The ability to setRawTags, setContentTags, and setEscapedTags have been removed from Laravel (laravel/framework#17736). They are depreciated here.
  • The compiler function setDeleteViewCacheAfterRender, has been deprecated as I didn't find any code where it was actually being used.
  • Added more tests

Installation

Add the package to composer.json:

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

To get versions 'composer show wpb/string-blade-compiler', such as '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'

On packagist.org at https://packagist.org/packages/wpb/string-blade-compiler

composer require "wpb/string-blade-compiler"

Configuration

In config\app.php, providers section:

Both the ServiceProvider and Facade Alias are auto registered by Laravel. There is no need to add them to the /config/app.php file.

Replace 'Illuminate\View\ViewServiceProvider::class' with 'Wpb\String_Blade_Compiler\ViewServiceProvider::class',

This version does not require you to remove the registration of the original view component. Upon ServiceProvider registration it will replace the view binds with its self.

Laravel's Autoloader

There currently is a issue in Laravel's preload process of ServiceProviders. Service providers that are registered with the autoloaded are instantiated before service providers that are set in /config/app.php. This may cause problems in prior versions of StringBladeCompiler. The version has been rewitten to account for the autoloading process.

A pull request that would load vendor service providers registerd in the config/app.php file before autoloads, was sent to Laravel/Framework and was rejected.

Config

Default cache time for the compiled string template is 300 seconds (5 mins), this can be changed in the config file, env file, or when calling a view. The change is global to all string templates.

STRING_BLADE_CACHE_TIMEOUT=300

Autoloading of blade custom directives can be changed in the config and env files.

STRING_BLADE_AUTOLOAD=false

Note: If using homestead or some other vm, the host handles the filemtime of the cache file. This means the vm may have a different time than the file. If the cache is not expiring as expected, check the times between the systems.

Usage

This package offers a StringView facade with the same syntax as View but accepts a Array or Array Object instance instead of path to view.

New Config Option:

Laravel 5.8 BladeCompiler adds a php comment to the compiled template file php $contents .= "<?php /**PATH {$this->getPath()} ENDPATH**/ ?>"; . Since StringBladeCompiler does not have a "path" aka "template file location" that would be helpful to the developer. I have included a new config value, templateRefKey. This allows the developer to tag the StringBladeCompiler for where it is used. This is for if you end up digging in to the compiled view files, it would allow you to see a tag for StingBladeCompiler files.

Config Options:

// 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'
            )
        );

Since StringBlade is a extend class from the original View. You should be able to do anything you would normally do with a View using StringBlade.

Blade::extend, for example :

As the compilers are set up as separate instances, if you need the extend on both the string and file template you will need to attach the extend (or directive) to both compilers.

// 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);
});

Other options,

// 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. __

Deleting generated compiled cach view files (v3+).Set the delete flag for the compiler being used, stringblade or blade

I can't seem to find when the setting was actually used. If you want this, submit a bug and I will see about adding the ability.

// 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);

License

string-blade-compiler is open-sourced software licensed under the MIT license

wpb/string-blade-compiler 适用场景与选型建议

wpb/string-blade-compiler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.09M 次下载、GitHub Stars 达 173, 最近一次更新时间为 2015 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「compiler」 「blade」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 wpb/string-blade-compiler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wpb/string-blade-compiler 我们能提供哪些服务?
定制开发 / 二次开发

基于 wpb/string-blade-compiler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.09M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 175
  • 点击次数: 21
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 173
  • Watchers: 6
  • Forks: 74
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-30