public function fetch($templateFile, $var, $charset, $varPrefix)
 {
     if (!$this->checkCache($templateFile)) {
         import('Think.Template.ThinkTemplate');
         $tpl = ThinkTemplate::getInstance();
         $tpl->load($templateFile, $charset, $var, $varPrefix);
     } else {
         extract($var, empty($varPrefix) ? EXTR_OVERWRITE : EXTR_PREFIX_ALL, $varPrefix);
         include CACHE_PATH . md5($templateFile) . C('CACHFILE_SUFFIX');
     }
 }
 /**
 +----------------------------------------------------------
 * 渲染模板输出
 +----------------------------------------------------------
 * @access public
 +----------------------------------------------------------
 * @param string $templateFile 模板文件名
 * @param array $var 模板变量
 * @param string $charset 模板输出字符集
 * @param string $varPrefix 模板变量前缀
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
 public function fetch($templateFile, $var, $charset)
 {
     if (!$this->checkCache($templateFile)) {
         // 缓存无效 重新编译
         import('ThinkTemplate');
         $tpl = ThinkTemplate::getInstance();
         // 编译并加载模板文件
         $tpl->load($templateFile, $var, $charset);
     } else {
         // 缓存有效 直接载入模板缓存
         // 模板阵列变量分解成为独立变量
         extract($var, EXTR_OVERWRITE);
         //载入模版缓存文件
         include CACHE_PATH . md5($templateFile) . C('CACHFILE_SUFFIX');
     }
 }
 public function __construct($tagLib = '', $filename = '')
 {
     if (empty($tagLib)) {
         $tagLib = strtolower(substr(get_class($this), 6));
     }
     $this->tagLib = $tagLib;
     $this->tpl = ThinkTemplate::getInstance();
     if (!empty($filename)) {
         $this->xml = $filename;
     } else {
         $this->xml = dirname(__FILE__) . '/Tags/' . $tagLib . '.xml';
     }
     if (method_exists($this, '_initialize')) {
         $this->_initialize();
     }
     $this->load();
 }
Esempio n. 4
0
 /**
 +----------------------------------------------------------
 * 加载模板和页面输出
 +----------------------------------------------------------
 * @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;
     }
 }
Esempio n. 5
0
 /**
 +----------------------------------------------------------
 * 架构函数
 +----------------------------------------------------------
 * @access public
 +----------------------------------------------------------
 */
 public function __construct()
 {
     $this->tagLib = strtolower(substr(get_class($this), 6));
     $this->tpl = ThinkTemplate::getInstance();
     $this->_initialize();
     $this->load();
 }
Esempio n. 6
0
 function ParseTemplateBehavior($_data){
     $_content = empty($_data['content']) ? $_data['file'] : $_data['content'];
     $_data['prefix'] = !empty($_data['prefix']) ? $_data['prefix'] : '';
     if ((!empty($_data['content']) && $this->checkContentCache($_data['content'], $_data['prefix'])) || $this->checkCache($_data['file'], $_data['prefix'])) {
         extract($_data['var'], EXTR_OVERWRITE);
         include $this->cache_path ."/". $_data['prefix'] . md5($_content) .$this->tmpl_cachfile_suffix;
     } else {
         require_once 'ThinkTemplate/ThinkTemplate.class.php';
         $tpl = new ThinkTemplate();
         $tpl->config = $this->config;
         $tpl->fetch($_content, $_data['var'], $_data['prefix']);
     }
 }
Esempio n. 7
0
 /**
 +----------------------------------------------------------
 * 加载模板和页面输出
 +----------------------------------------------------------
 * @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;
     }
 }