| | <?php |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | namespace think\view\driver; |
| |
|
| | use think\App; |
| | use think\exception\TemplateNotFoundException; |
| | use think\Loader; |
| | use think\Log; |
| | use think\Request; |
| | use think\Template; |
| |
|
| | class Think |
| | { |
| | |
| | private $template; |
| | |
| | protected $config = [ |
| | |
| | 'view_base' => '', |
| | |
| | 'view_path' => '', |
| | |
| | 'view_suffix' => 'html', |
| | |
| | 'view_depr' => DS, |
| | |
| | 'tpl_cache' => true, |
| | |
| | 'auto_rule' => 1, |
| | ]; |
| |
|
| | public function __construct($config = []) |
| | { |
| | $this->config = array_merge($this->config, $config); |
| | if (empty($this->config['view_path'])) { |
| | $this->config['view_path'] = App::$modulePath . 'view' . DS; |
| | } |
| |
|
| | $this->template = new Template($this->config); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | public function exists($template) |
| | { |
| | if ('' == pathinfo($template, PATHINFO_EXTENSION)) { |
| | |
| | $template = $this->parseTemplate($template); |
| | } |
| | return is_file($template); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | public function fetch($template, $data = [], $config = []) |
| | { |
| | if ('' == pathinfo($template, PATHINFO_EXTENSION)) { |
| | |
| | $template = $this->parseTemplate($template); |
| | } |
| | |
| | if (!is_file($template)) { |
| | |
| | if (strpos($template, '/view_new') !== false) { |
| | $sourcePath = str_replace('/view_new', '/view', $template); |
| | if (file_exists($sourcePath)) { |
| | if (!copy($sourcePath, $template)) { |
| | throw new TemplateNotFoundException('Please copy file:' . $sourcePath . ' => ' . $template); |
| | } |
| | } |
| | }else{ |
| | throw new TemplateNotFoundException('template not exists:' . $template, $template); |
| | } |
| | } |
| | |
| | App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); |
| | $this->template->fetch($template, $data, $config); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | public function display($template, $data = [], $config = []) |
| | { |
| | $this->template->display($template, $data, $config); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | private function parseTemplate($template) |
| | { |
| | |
| | $request = Request::instance(); |
| | |
| | if (strpos($template, '@')) { |
| | |
| | list($module, $template) = explode('@', $template); |
| | } |
| | if ($this->config['view_base']) { |
| | |
| | $module = isset($module) ? $module : $request->module(); |
| | $path = $this->config['view_base'] . ($module ? $module . DS : ''); |
| | } else { |
| | $path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : $this->config['view_path']; |
| | } |
| |
|
| | $depr = $this->config['view_depr']; |
| | if (0 !== strpos($template, '/')) { |
| | $template = str_replace(['/', ':'], $depr, $template); |
| | $controller = Loader::parseName($request->controller()); |
| | if ($controller) { |
| | if ('' == $template) { |
| | |
| | $template = str_replace('.', DS, $controller) . $depr . (1 == $this->config['auto_rule'] ? Loader::parseName($request->action(true)) : $request->action()); |
| | } elseif (false === strpos($template, $depr)) { |
| | $template = str_replace('.', DS, $controller) . $depr . $template; |
| | } |
| | } |
| | } else { |
| | $template = str_replace(['/', ':'], $depr, substr($template, 1)); |
| | } |
| | return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.'); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | public function config($name, $value = null) |
| | { |
| | if (is_array($name)) { |
| | $this->template->config($name); |
| | $this->config = array_merge($this->config, $name); |
| | } elseif (is_null($value)) { |
| | return $this->template->config($name); |
| | } else { |
| | $this->template->$name = $value; |
| | $this->config[$name] = $value; |
| | } |
| | } |
| |
|
| | public function __call($method, $params) |
| | { |
| | return call_user_func_array([$this->template, $method], $params); |
| | } |
| | } |
| |
|