root(); // $root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base; // if ('' != $root) { // $root = '/' . ltrim($root, '/'); // } // $baseReplace = [ // '__ROOT__' => $root, // '__URL__' => $base . '/' . $request->module() . '/' . Loader::parseName($request->controller()), // '__STATIC__' => $root . '/static', // '__CSS__' => $root . '/static/css', // '__JS__' => $root . '/static/js', // ]; // $this->replace = array_merge($baseReplace, (array) $replace); // } // /** // * 初始化视图 // * @access public // * @param array $engine 模板引擎参数 // * @param array $replace 字符串替换参数 // * @return object // */ // public static function instance($engine = [], $replace = []) // { // if (is_null(self::$instance)) { // self::$instance = new self($engine, $replace); // } // return self::$instance; // } public static function instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /** * 变量名替换 * @param $name * @param $value */ public function assign($name, $value) { if (is_array($name)) { $this->data = array_merge($this->data, $name); } else { $this->data[$name] = $value; } return $this; } /** * 显示视图 * @param $file */ // public function display($file) // { // $file = APP.'/views/'.$file; // if (is_file($file)){ // extract($this->assign);#从数组中将变量导入到当前的符号表 // include $file; // } // } public function fetch($file = "") { // 页面缓存 ob_start(); ob_implicit_flush(0); if (!empty($this->data)) { extract($this->data); } if ($file) { $namearary = explode(".", $file); $typename = end($namearary); if (!in_array($typename, ['html', 'tpl', 'htm', 'php'])) { $file = $file . '.' . TPL; } $filename = APP_PATH . "/" . $GLOBALS['model'] . "/view/" . strtolower($GLOBALS['ctrl']) . "/" . $file; require($filename); } else { require(APP_PATH . DS . $GLOBALS['model'] . DS . 'view' . DS . strtolower($GLOBALS['ctrl']) . DS . $GLOBALS['action'] . TPL); } // 获取并清空缓存 $content = ob_get_clean(); return $content; } }