ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 视图前言 模板引擎有很多种,这里使用的是`laravel` 的模板引擎。 ``` // 安装laravel的blade模板引擎 composer require duncan3dc/blade ``` 详见:[duncan3dc/blade](https://packagist.org/packages/duncan3dc/blade) ## 创建视图配置config/view.php ``` <?php return [ // 模板缓存路径 'cache_path' => FRAME_BASE_PATH . '/views/cache', // 模板的根目录 'view_path' => FRAME_BASE_PATH . '/views/' ]; ``` ## 创建接口 core/view/ViewInterface.php ``` <?php namespace core\view; interface ViewInterface { // 初始化模板 public function init(); // 解析模板模板 function render($path); } ``` ## laravel blade的实现 创建core/view/Blade.php ``` <?php namespace core\view; use duncan3dc\Laravel\BladeInstance; class Blade implements ViewInterface { protected $template; public function init() { $config = \App::getContainer()->get('config')->get('view'); // 获取配置 // 设置视图路径 和 缓存路径 // 用法见: duncan3dc/blade $this->template = new BladeInstance($config['view_path'], $config['cache_path']); } // 传递路径 和 参数 public function render($path, $params = []) { return $this->template->render($path, $params); } } ``` ## 绑定服务 契约 ![](https://img.kancloud.cn/7a/7e/7a7e185acb8aff2cf7899d660e143ab0_678x298.png) ## 视图初始化 ![](https://img.kancloud.cn/ad/8a/ad8a4a56ae00bf332d176de0bb903e9f_736x126.png) 现在视图已经完成了,接下来是运行下。 ## 运行 ### 创建views/blade/index.blade.php ``` <h2>{{ $str }}</h2> @include('blade.footer') ``` ### 创建views/blade/footer.blade.php ``` <h1> FOOTER </h1> ``` ### 路由运行 ![](https://img.kancloud.cn/f9/17/f91775dfb3c26767c3d4fc021ae68ec7_800x379.png) ![](https://img.kancloud.cn/d1/e9/d1e903875837413ac0a5f7953535341b_530x390.png)