Laravel用习惯了以后,再用别的小框架总是想把Laravel的东西摘过去。。。这次摘一下视图层view
目录结构
├── compiled │ └── .gitkeep ├── composer.json ├── composer.lock ├── start.php └── views └── welcome.blade.php
安装composer包:
composer require illuminate/view 5.7.\*
关键代码:
start.php
<?php /** * Created by PhpStorm. * User: reatang * Date: 2019/3/15 * Time: 下午6:26 * Author reatang */ include __DIR__ . '/vendor/autoload.php'; // 视图系统 use \Illuminate\View\Engines\EngineResolver; use \Illuminate\View\Engines\FileEngine; use \Illuminate\View\Engines\PhpEngine; use \Illuminate\View\Compilers\BladeCompiler; use \Illuminate\View\Engines\CompilerEngine; use \Illuminate\View\FileViewFinder; use \Illuminate\View\Factory; use \Illuminate\View\View; //文件系统 use \Illuminate\Filesystem\Filesystem; //事件系统 use \Illuminate\Events\Dispatcher; /** * 配置 */ // 模板文件目录 $config['view.paths'] = [__DIR__ . '/views/']; // 编译模板缓存目录 $config['view.compiled'] = __DIR__ . '/compiled/'; /** * 文件系统抽象 */ $GLOBALS['__files'] = new Filesystem(); /** * 事件管理器 */ $GLOBALS['__events'] = new Dispatcher(); /** * 模板编译器 */ $resolver = new EngineResolver(); $resolver->register('file', function () { return new FileEngine(); }); $resolver->register('php', function () { return new PhpEngine(); }); $resolver->register('blade', function () use ($config) { $blade_compiler = new BladeCompiler( $GLOBALS['__files'], $config['view.compiled'] ); return new CompilerEngine($blade_compiler); }); /** * 模板文件管理器 */ $finder = new FileViewFinder($GLOBALS['__files'], $config['view.paths']); /** * 视图工厂 */ $factory = new Factory($resolver, $finder, $GLOBALS['__events']); /** * 命令 */ /** * 清除视图缓存 */ function view_clear() { /** * @var array $config * @var Filesystem $__files */ global $config, $__files; $path = $config['view.compiled']; if (! $path) { throw new RuntimeException('View path not found.'); } foreach ($__files->glob("{$path}/*") as $view) { if (strpos($view, 'gitkeep') === false) { $__files->delete($view); } } echo 'Compiled views cleared!' . PHP_EOL; } /** * 初始化完成 * * 弱化: * 1、无容器,所以无法使用 class型的事件,只能用匿名函数 * 2、无多语言,未加载多语言组件(因为也需要容器),无法使用 @lang @trans * 3、没有auth系统,所以无法用 @auth @guest 之类的认证功能 * 4、没有session功能,所有无法使用 @csrf 功能 * 5、没有容器,无法使用 @inject 功能 * 6、没有LaravelCore不能使用 @method * 7、还有未知的弱化内容未发现.... */ // 扩展 Blade /** @var BladeCompiler $_compiler */ $_compiler = $resolver->resolve('blade')->getCompiler(); $_compiler->directive('lalala', function ($expression) { return "<?php echo ($expression)->format('m/d/Y H:i'); ?>"; }); // 模板事件可注入参数 模板文件支持通配符 $factory->creator('welcome', function (View $view) { // creator 是在视图被make的时候立马触发 $view->with('creator_var', 'creator 注入'); }); $factory->composer('welcome', function (View $view) { // composer 是在视图render时触发 $view->with('composer_var', 'composer 注入'); }); //共享变量特性 $factory->share('global_var1', '全局共享变量'); //渲染 echo $factory->make('welcome')->render();
welcome.blade.php
<?php $hello = '你好' ?> <!doctype html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>你好 Blade</title> </head> <body> <div> {{ $hello }} </div> <ul> @foreach ([1,2,3] as $v) <li>{{ $v }}</li> @endforeach </ul> <div> {{ $global_var1 }} </div> <div> {{ $creator_var }} </div> <div> {{ $composer_var }} </div> <div> @lalala(\Carbon\Carbon::now()) </div> </body> </html>