/** * 获取处理后的模板文件路径 * * @param string $srcTplpath * tpl文件如/path/new.tpl * @param boolean $useCache * 是否使用已存在的编译后的文件,默认为true * @throws Exception * @return string */ public static function includeTpl($srcTplpath, $useCache = true) { if (!defined('IN_TEMPLATE_L')) { define('IN_TEMPLATE_L', true); } $tpl = new self(); $tplPath = $tpl->getTplRealpath($srcTplpath); if (!is_file($tplPath)) { throw new Exception('template[' . $srcTplpath . '] not found in ' . $tplPath); } $distPath = $tpl->getTplRealpath($srcTplpath, false); // if (defined('APP_DEBUG')) { $useCache = false; } // 如果预处理后的文件存在,且指定使用缓存,则直接返回路径 if ($useCache && is_file($distPath)) { return $distPath; } $distPathDir = dirname($distPath); if (!is_dir($distPathDir)) { if (!mkdir($distPathDir, 0777, true)) { throw new Exception('failed to create dist dir ' . $distPathDir . ' for template[' . $srcTplpath . ']'); } } $content = $tpl->generate($srcTplpath); $handle = @fopen($distPath, 'w'); if ($handle === false) { throw new Exception('failed to write data to ' . $distPath . ' for template[' . $srcTplpath . ']'); } else { fwrite($handle, $content); fclose($handle); } return $distPath; }