/** +---------------------------------------------------------- * 输出模板 +---------------------------------------------------------- * @access protected +---------------------------------------------------------- * @param string $content 模板内容 * @param boolean $display 是否直接显示 +---------------------------------------------------------- * @return mixed +---------------------------------------------------------- */ protected function output($content, $display) { if (C('HTML_CACHE_ON')) { HtmlCache::writeHTMLCache($content); } if ($display) { if (false !== strpos($content, '{__RUNTIME__}')) { $runtime = C('SHOW_RUN_TIME') ? '<div id="think_run_time" class="think_run_time">' . $this->showTime() . '</div>' : ''; $content = str_replace('{__RUNTIME__}', $runtime, $content); } echo $content; if (C('SHOW_PAGE_TRACE')) { $this->showTrace(); } return null; } else { return $content; } }
/** +---------------------------------------------------------- * 加载模板和页面输出 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $templateFile 模板文件名 留空为自动获取 * @param string $charset 模板输出字符集 * @param string $contentType 输出类型 * @param string $varPrefix 模板变量前缀 * @param integer $mode 0 返回 1 输出 2 下载 +---------------------------------------------------------- * @throws ThinkExecption +---------------------------------------------------------- */ public function fetch($templateFile = '', $charset = '', $contentType = 'text/html', $varPrefix = '', $display = false) { $startTime = array_sum(explode(' ', microtime())); if (null === $templateFile) { // 使用null参数作为模版名直接返回不做任何输出 return; } if ('layout::' == substr($templateFile, 0, 8)) { $this->layout(substr($templateFile, 8)); return; } if (empty($charset)) { $charset = C('OUTPUT_CHARSET'); } // 网页字符编码 header("Content-Type:" . $contentType . "; charset=" . $charset); header("Cache-control: private"); //支持页面回跳 // 设置输出缓存 ini_set('output_buffering', 4096); $zlibCompress = ini_get('zlib.output_compression'); if (empty($zlibCompress) && function_exists('ini_set')) { ini_set('zlib.output_compression', 1); } // 缓存初始化过滤 apply_filter('ob_init'); //页面缓存 ob_start(); ob_implicit_flush(0); // 缓存开启后执行的过滤 apply_filter('ob_start'); // 模版文件名过滤 $templateFile = apply_filter('template_file', $templateFile); if ('' == $templateFile) { // 如果模板文件名为空 按照默认规则定位 $templateFile = C('TMPL_FILE_NAME'); } elseif (strpos($templateFile, '@')) { // 引入其它主题的操作模板 必须带上模块名称 例如 blue@User:add $templateFile = TMPL_PATH . '/' . str_replace(array('@', ':'), '/', $templateFile) . C('TEMPLATE_SUFFIX'); } elseif (strpos($templateFile, ':')) { // 引入其它模块的操作模板 $templateFile = TEMPLATE_PATH . '/' . str_replace(':', '/', $templateFile) . C('TEMPLATE_SUFFIX'); } elseif (!file_exists($templateFile)) { // 引入当前模块的其它操作模板 $templateFile = dirname(C('TMPL_FILE_NAME')) . '/' . $templateFile . C('TEMPLATE_SUFFIX'); } if (!file_exists($templateFile)) { throw_exception(L('_TEMPLATE_NOT_EXIST_')); } // 模版变量过滤 $this->tVar = apply_filter('template_var', $this->tVar); //根据不同模版引擎进行处理 if ('PHP' == $this->type || empty($this->type)) { // 默认使用PHP模版 include $templateFile; } elseif ('THINK' == $this->type) { // 使用内置的ThinkTemplate模板引擎 if (!$this->checkCache($templateFile)) { // 缓存无效 重新编译 $compiler = true; import('Think.Template.ThinkTemplate'); $tpl = new ThinkTemplate(); // 编译并加载模板文件 $tpl->load($templateFile, $charset, $this->tVar, $varPrefix); } else { // 缓存有效 直接载入模板缓存 // 模板阵列变量分解成为独立变量 extract($this->tVar, empty($varPrefix) ? EXTR_OVERWRITE : EXTR_PREFIX_ALL, $varPrefix); //载入模版缓存文件 include CACHE_PATH . md5($templateFile) . C('CACHFILE_SUFFIX'); } } else { // 通过插件的方式扩展第三方模板引擎 use_compiler(C('TMPL_ENGINE_TYPE'), $templateFile, $this->tVar, $charset, $varPrefix); } // 获取并清空缓存 $content = ob_get_clean(); // 输出编码转换 $content = auto_charset($content, C('TEMPLATE_CHARSET'), $charset); // 输出过滤 $content = apply_filter('ob_content', $content); if (C('HTML_CACHE_ON')) { // 写入静态文件 HtmlCache::writeHTMLCache($content); } if ($display) { $showTime = $this->showTime($startTime); echo $content; if (C('SHOW_RUN_TIME')) { echo '<div class="think_run_time">' . $showTime . '</div>'; } if (C('SHOW_PAGE_TRACE')) { // 显示页面Trace信息 读取Trace定义文件 // 定义格式 return array('当前页面'=>$_SERVER['PHP_SELF'],'通信协议'=>$_SERVER['SERVER_PROTOCOL'],...); $traceFile = CONFIG_PATH . '_trace.php'; if (file_exists($traceFile)) { $_trace = (include $traceFile); } else { $_trace = array(); } // 系统默认显示信息 $this->trace('当前页面', $_SERVER['PHP_SELF']); $this->trace('请求方法', $_SERVER['REQUEST_METHOD']); $this->trace('通信协议', $_SERVER['SERVER_PROTOCOL']); $this->trace('请求时间', Date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME'])); $this->trace('用户代理', $_SERVER['HTTP_USER_AGENT']); $this->trace('会话ID', session_id()); $this->trace('运行数据', $showTime); $this->trace('输出编码', $charset); $this->trace('加载类库', $GLOBALS['include_file']); $this->trace('模板编译', !empty($compiler) ? '重新编译' : '读取缓存'); if (isset(Log::$log[SQL_LOG_DEBUG])) { $log = Log::$log[SQL_LOG_DEBUG]; $this->trace('SQL记录', is_array($log) ? count($log) . '条SQL<br/>' . implode('<br/>', $log) : '无SQL记录'); } else { $this->trace('SQL记录', '无SQL记录'); } $this->trace('错误记录', count(App::$debug) . '条注意<br/>' . implode('<br/>', App::$debug)); $_trace = array_merge($_trace, $this->trace); $_trace = auto_charset($_trace, 'utf-8'); $_title = auto_charset('页面Trace信息', 'utf-8'); // 调用Trace页面模板 include THINK_PATH . '/Tpl/PageTrace.tpl.php'; } return null; } else { return $content; } }
/** +---------------------------------------------------------- * 输出模板 +---------------------------------------------------------- * @access protected +---------------------------------------------------------- * @param string $content 模板内容 * @param boolean $display 是否直接显示 +---------------------------------------------------------- * @return mixed +---------------------------------------------------------- */ protected function output($content, $display) { if (C('HTML_CACHE_ON')) { HtmlCache::writeHTMLCache($content); } if ($display) { if (C('SHOW_RUN_TIME')) { if (false !== strpos($content, '{__NORUNTIME__}')) { $content = str_replace('{__NORUNTIME__}', '', $content); } else { $runtime = $this->showTime(); if (strpos($content, '{__RUNTIME__}')) { $content = str_replace('{__RUNTIME__}', $runtime, $content); } else { $content .= $runtime; } } } else { $content = str_replace(array('{__NORUNTIME__}', '{__RUNTIME__}'), '', $content); } echo $content; if (C('SHOW_PAGE_TRACE')) { $this->showTrace(); } return null; } else { return $content; } }
/** +---------------------------------------------------------- * 输出模板 +---------------------------------------------------------- * @access protected +---------------------------------------------------------- * @param string $content 模板内容 * @param boolean $display 是否直接显示 +---------------------------------------------------------- * @return mixed +---------------------------------------------------------- */ protected function output($content, $display) { if (C('HTML_CACHE_ON')) { // 写入静态文件 HtmlCache::writeHTMLCache($content); } // 视图输出标签 if (C('TAG_PLUGIN_ON')) { tag('view_output', array($content)); } if ($display) { $showTime = $this->showTime(); echo $content; if (C('SHOW_RUN_TIME')) { echo '<div id="think_run_time" class="think_run_time">' . $showTime . '</div>'; } $this->showTrace($showTime); return null; } else { return $content; } }
protected function output($content, $display, $charset, $htmlCache = true) { if (C('HTML_CACHE_ON') && $htmlCache) { HtmlCache::writeHTMLCache($content); } if ($display) { $showTime = $this->showTime(); echo $content; if (C('SHOW_RUN_TIME')) { echo '<div id="think_run_time" class="think_run_time">' . $showTime . '</div>'; } $this->showTrace($showTime, $charset); return null; } else { return $content; } }
/** +---------------------------------------------------------- * 加载模板和页面输出 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $templateFile 模板文件名 留空为自动获取 * @param string $charset 模板输出字符集 * @param string $contentType 输出类型 * @param string $varPrefix 模板变量前缀 * @param integer $mode 0 返回 1 输出 2 下载 +---------------------------------------------------------- * @throws ThinkExecption +---------------------------------------------------------- */ public function fetch($templateFile = '', $charset = '', $contentType = 'text/html', $varPrefix = '', $display = false) { $startTime = microtime(TRUE); if (null === $templateFile) { // 使用null参数作为模版名直接返回不做任何输出 return; } if ('layout::' == substr($templateFile, 0, 8)) { $this->layout(substr($templateFile, 8)); return; } if (empty($charset)) { $charset = C('OUTPUT_CHARSET'); } // 网页字符编码 header("Content-Type:" . $contentType . "; charset=" . $charset); header("Cache-control: private"); //支持页面回跳 // 设置输出缓存 ini_set('output_buffering', 4096); $zlibCompress = ini_get('zlib.output_compression'); if (empty($zlibCompress) && function_exists('ini_set')) { ini_set('zlib.output_compression', 1); } $pluginOn = C('THINK_PLUGIN_ON'); if ($pluginOn) { // 缓存初始化过滤 apply_filter('ob_init'); } //页面缓存 ob_start(); ob_implicit_flush(0); if ($pluginOn) { // 缓存开启后执行的过滤 apply_filter('ob_start'); // 模版文件名过滤 $templateFile = apply_filter('template_file', $templateFile); } if ('' == $templateFile) { // 如果模板文件名为空 按照默认规则定位 $templateFile = C('TMPL_FILE_NAME'); } elseif (strpos($templateFile, '@')) { // 引入其它主题的操作模板 必须带上模块名称 例如 blue@User:add $templateFile = TMPL_PATH . str_replace(array('@', ':'), '/', $templateFile) . C('TEMPLATE_SUFFIX'); } elseif (strpos($templateFile, ':')) { // 引入其它模块的操作模板 $templateFile = TEMPLATE_PATH . '/' . str_replace(':', '/', $templateFile) . C('TEMPLATE_SUFFIX'); } elseif (!file_exists($templateFile)) { // 引入当前模块的其它操作模板 $templateFile = dirname(C('TMPL_FILE_NAME')) . '/' . $templateFile . C('TEMPLATE_SUFFIX'); } if (!file_exists_case($templateFile)) { throw_exception(L('_TEMPLATE_NOT_EXIST_') . '[' . $templateFile . ']'); } if ($pluginOn) { // 模版变量过滤 $this->tVar = apply_filter('template_var', $this->tVar); } $compiler = false; //根据不同模版引擎进行处理 if ('PHP' == $this->type || empty($this->type)) { // 模板阵列变量分解成为独立变量 extract($this->tVar, empty($varPrefix) ? EXTR_OVERWRITE : EXTR_PREFIX_ALL, $varPrefix); // 默认使用PHP模版 include $templateFile; } elseif ('THINK' == $this->type) { // 使用内置的ThinkTemplate模板引擎 if (!$this->checkCache($templateFile)) { // 缓存无效 重新编译 $compiler = true; import('Think.Template.ThinkTemplate'); $tpl = ThinkTemplate::getInstance(); // 编译并加载模板文件 $tpl->load($templateFile, $charset, $this->tVar, $varPrefix); } else { // 缓存有效 直接载入模板缓存 // 模板阵列变量分解成为独立变量 extract($this->tVar, empty($varPrefix) ? EXTR_OVERWRITE : EXTR_PREFIX_ALL, $varPrefix); //载入模版缓存文件 include CACHE_PATH . md5($templateFile) . C('CACHFILE_SUFFIX'); } } elseif ($pluginOn) { // 通过插件的方式扩展第三方模板引擎 use_compiler(C('TMPL_ENGINE_TYPE'), $templateFile, $this->tVar, $charset, $varPrefix); } // 获取并清空缓存 $content = ob_get_clean(); // 输出编码转换 $content = auto_charset($content, C('TEMPLATE_CHARSET'), $charset); if ($pluginOn) { // 输出过滤 $content = apply_filter('ob_content', $content); } if (C('HTML_CACHE_ON')) { // 写入静态文件 HtmlCache::writeHTMLCache($content); } if ($display) { $showTime = $this->showTime($startTime); echo $content; if (C('SHOW_RUN_TIME')) { echo '<div id="think_run_time" class="think_run_time">' . $showTime . '</div>'; } $this->showTrace($showTime, $charset, $compiler); return null; } else { return $content; } }