├── .gitignore ├── composer.json ├── README.md └── drivers └── thinkphp5 └── Blade.php /.gitignore: -------------------------------------------------------------------------------- 1 | *.buildpath 2 | .settings 3 | .project 4 | .idea 5 | /.idea 6 | /vendor 7 | composer.lock 8 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "terranc/think-blade", 3 | "description": "think blade view engine", 4 | "license": "Apache-2.0", 5 | "authors": [ 6 | { 7 | "name": "terranc", 8 | "email": "terran.chao@lookfeel.co" 9 | } 10 | ], 11 | "support": { 12 | "issues": "https://github.com/terranc/think-blade/issues" 13 | }, 14 | "require": { 15 | "php": ">=5.4.0", 16 | "terranc/blade": "^0.1" 17 | }, 18 | "autoload": { 19 | "classmap": [ 20 | { 21 | "think\\view\\driver\\Blade": "drivers/thinkphp5/Blade.php" 22 | } 23 | ] 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Think-Blade 2 | Blade template engine with thinkphp 5. (component & slot support) 3 | 4 | # Installation 5 | composer require terranc/think-blade 6 | 7 | conig.php: 8 | ```php 9 | 'template' => [ 10 | // 模板引擎类型 支持 php think 支持扩展 11 | 'type' => 'Blade', 12 | // 模板路径 13 | 'view_path' => '', 14 | // 模板后缀 15 | 'view_suffix' => 'blade.php', 16 | // 模板文件名分隔符 17 | 'view_depr' => DIRECTORY_SEPARATOR, 18 | // 模板引擎普通标签开始标记 19 | 'tpl_begin' => '{{', 20 | // 模板引擎普通标签结束标记 21 | 'tpl_end' => '}}', 22 | 'tpl_raw_begin' => '{!!', 23 | 'tpl_raw_end' => '{!!', 24 | // 标签库标签开始标记 25 | 'taglib_begin' => '{', 26 | // 标签库标签结束标记 27 | 'taglib_end' => '}', 28 | ], 29 | ``` 30 | 31 | # Usage 32 | ```html 33 | 49 | ``` 50 | 51 | # DOC 52 | 53 | https://laravel.com/docs/5.4/blade 54 | 55 | http://d.laravel-china.org/docs/5.4/blade (中文) 56 | -------------------------------------------------------------------------------- /drivers/thinkphp5/Blade.php: -------------------------------------------------------------------------------- 1 | '', 22 | // 是否开启模板编译缓存,设为false则每次都会重新编译 23 | 'tpl_cache' => true, 24 | // 模板起始路径 25 | 'view_path' => '', 26 | 'tpl_begin' => '{{', 27 | 'tpl_end' => '}}', 28 | 'tpl_raw_begin' => '{!!', 29 | 'tpl_raw_end' => '!!}', 30 | // 模板文件后缀 31 | 'view_suffix' => 'blade.php', 32 | ]; 33 | public function __construct($config = []) 34 | { 35 | $this->config($config); 36 | } 37 | private function boot($config = []) { 38 | $this->config = array_merge($this->config, $config); 39 | if (empty($this->config['view_path'])) { 40 | $this->config['view_path'] = app()->getModulePath() . 'view' . DIRECTORY_SEPARATOR; 41 | } 42 | $this->config['view_cache_path'] = Env::get('runtime_path') . 'temp' . DIRECTORY_SEPARATOR; 43 | $compiler = new BladeCompiler($this->config['view_cache_path'], $this->config['tpl_cache']); 44 | $compiler->setContentTags($this->config['tpl_begin'], $this->config['tpl_end'], true); 45 | $compiler->setContentTags($this->config['tpl_begin'], $this->config['tpl_end'], false); 46 | $compiler->setRawTags($this->config['tpl_raw_begin'], $this->config['tpl_raw_end'], false); 47 | $engine = new CompilerEngine($compiler); 48 | $finder = new FileViewFinder([$this->config['view_path']], [$this->config['view_suffix'], 'tpl']); 49 | // 实例化 Factory 50 | $this->template = new Factory($engine, $finder); 51 | } 52 | /** 53 | * 检测是否存在模板文件 54 | * @access public 55 | * @param string $template 模板文件或者模板规则 56 | * @return bool 57 | */ 58 | public function exists($template) 59 | { 60 | if ('' == pathinfo($template, PATHINFO_EXTENSION)) { 61 | // 获取模板文件名 62 | $template = $this->parseTemplate($template); 63 | } 64 | return is_file($template); 65 | } 66 | /** 67 | * 渲染模板文件 68 | * @access public 69 | * @param string $template 模板文件 70 | * @param array $data 模板变量 71 | * @param array $mergeData 附加变量 72 | * @param array $config 参数 73 | * @return void 74 | */ 75 | public function fetch($template, $data = [], $mergeData = [], $config = []) 76 | { 77 | $this->config($config); 78 | if ('' == pathinfo($template, PATHINFO_EXTENSION)) { 79 | // 获取模板文件名 80 | $template = $this->parseTemplate($template); 81 | } 82 | // 模板不存在 抛出异常 83 | if (!is_file($template)) { 84 | throw new TemplateNotFoundException('template not exists:' . $template, $template); 85 | } 86 | // 记录视图信息 87 | app()->isDebug() && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); 88 | echo $this->template->file($template, $data, $mergeData)->render(); 89 | } 90 | /** 91 | * 渲染模板内容 92 | * @access public 93 | * @param string $template 模板内容 94 | * @param array $data 模板变量 95 | * @param array $mergeData 附加变量 96 | * @param array $config 参数 97 | * @return void 98 | */ 99 | public function display($template, $data = [], $mergeData = [], $config = []) 100 | { 101 | $this->config($config); 102 | return $this->template->make($template, $data, $mergeData)->render(); 103 | } 104 | /** 105 | * 自动定位模板文件 106 | * @access private 107 | * @param string $template 模板文件规则 108 | * @return string 109 | */ 110 | private function parseTemplate($template) 111 | { 112 | // 分析模板文件规则 113 | $request = request(); 114 | // 获取视图根目录 115 | if (strpos($template, '@')) { 116 | // 跨模块调用 117 | list($module, $template) = explode('@', $template); 118 | } 119 | if ($this->config['view_base']) { 120 | // 基础视图目录 121 | $module = isset($module) ? $module : $request->module(); 122 | $path = $this->config['view_base'] . ($module ? $module . DIRECTORY_SEPARATOR : ''); 123 | } else { 124 | $path = isset($module) ? Env::get('app_path') . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR : $this->config['view_path']; 125 | } 126 | $depr = $this->config['view_depr']; 127 | if (0 !== strpos($template, '/')) { 128 | $template = str_replace(['/', ':'], $depr, $template); 129 | $controller = Loader::parseName($request->controller()); 130 | if ($controller) { 131 | if ('' == $template) { 132 | // 如果模板文件名为空 按照默认规则定位 133 | $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $request->action(); 134 | } elseif (false === strpos($template, $depr)) { 135 | $template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template; 136 | } 137 | } 138 | } else { 139 | $template = str_replace(['/', ':'], $depr, substr($template, 1)); 140 | } 141 | return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.'); 142 | } 143 | /** 144 | * 配置或者获取模板引擎参数 145 | * @access private 146 | * @param string|array $name 参数名 147 | * @param mixed $value 参数值 148 | * @return mixed 149 | */ 150 | public function config($name, $value = null) 151 | { 152 | if (is_array($name)) { 153 | $this->config = array_merge($this->config, $name); 154 | } elseif (is_null($value)) { 155 | return $this->config[$name]; 156 | } else { 157 | $this->config[$name] = $value; 158 | } 159 | $this->boot(); 160 | } 161 | public function __call($method, $params) 162 | { 163 | return call_user_func_array([$this->template, $method], $params); 164 | } 165 | } 166 | --------------------------------------------------------------------------------