<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] * * * * * # 1 模板引擎 >>模板引擎,負責將模板變量填充到模板文件中, >默認模板引擎實現在\view\Think.php文件中 >tp5還支持php文件的模板,其接口與Think模板引擎一致。具體見\view\Php.php文件。 ### $think->__construct() >>模板引擎構造函數,創建模板引擎對象 ~~~ 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); } ~~~ >分析代碼可知,模板引擎的最終實現在Template.php文件中 >Think.php文件只是模板引擎的接口封裝 ### $think->config() >>模板引擎配置,修改模板引擎配置參數 ~~~ public function config($name, $value = null) { if (is_array($name)) { $this->template->config($name); } else { $this->template->$name = $value; } } ~~~ # 2 模板引擎解析 >> 正如上面所說,模板引擎負責將模板文件和模板變量解析為Web頁面,因此模板引擎的主要接口就是模板文件的解析操作。 ## 2-1 模板解析接口 ### $think->fetch() >> 獲取模板文件解析結果 ~~~ public function fetch($template, $data = [], $config = []) { if ('' == pathinfo($template, PATHINFO_EXTENSION)) { // 獲取模板文件名 $template = $this->parseTemplate($template); } // 模板不存在 拋出異常 if (!is_file($template)) { 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); } ~~~ ### $think->display() >> 輸出模板文件解析結果 ~~~ public function display($template, $data = [], $config = []) { $this->template->display($template, $data, $config); } ~~~ ## 2-2 模板解析輔助 ### $think->parseTemplate() >>定位模板文件 ~~~ private function parseTemplate($template) { // 獲取視圖根目錄 if (strpos($template, '@')) { // 跨模塊調用 list($module, $template) = explode('@', $template); $path = APP_PATH . $module . DS . 'view' . DS; } else { // 當前視圖目錄 $path = $this->config['view_path']; } // 分析模板文件規則 $request = Request::instance(); $controller = Loader::parseName($request->controller()); if ($controller && 0 !== strpos($template, '/')) { $depr = $this->config['view_depr']; $template = str_replace(['/', ':'], $depr, $template); if ('' == $template) { // 如果模板文件名為空 按照默認規則定位 $template = str_replace('.', DS, $controller) . $depr . $request->action(); } elseif (false === strpos($template, $depr)) { $template = str_replace('.', DS, $controller) . $depr . $template; } } return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.'); } ~~~ ### $think->exists() >>檢查模板文件是否存在 ~~~ public function exists($template) { if ('' == pathinfo($template, PATHINFO_EXTENSION)) { // 獲取模板文件名 $template = $this->parseTemplate($template); } return is_file($template); } ~~~ # 3 內置模板引擎(Think)實現(Template.php) >>內置模板引擎類型為配置文件中的Think.其實現在Template.php文件中 ## 3-1 模板引擎 ### $template->__construct() >>創建模板引擎對象 ~~~ public function __construct(array $config = []) { $this->config['cache_path'] = TEMP_PATH; $this->config = array_merge($this->config, $config); $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']); $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']); $this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']); // 初始化模板編譯存儲器 $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File'; $class = false !== strpos($type, '\\') ? $type : '\\think\\template\\driver\\' . ucwords($type); $this->storage = new $class(); } ~~~ >由上可知,創建模板引擎對象時,可以指定模板編譯結果存儲方式,默認為File,使用本地文件存儲,其實現在template\driver\File.php文件 ### $template->config() >>獲取或修改模板引擎的配置參數 ~~~ public function config($config) { if (is_array($config)) { $this->config = array_merge($this->config, $config); } elseif (isset($this->config[$config])) { return $this->config[$config]; } } ~~~ ## 3-2 模板引擎變量操作 >>模板引擎 可以存儲將要填充的數據到模板變量中 ### $template->assign() >> 存儲數據到模板變量中 ~~~ public function assign($name, $value = '') { if (is_array($name)) { $this->data = array_merge($this->data, $name); } else { $this->data[$name] = $value; } } ~~~ ### $template->get() >> 讀取模板變量的數據值 ~~~ public function get($name = '') { if ('' == $name) { return $this->data; } else { $data = $this->data; foreach (explode('.', $name) as $key => $val) { if (isset($data[$val])) { $data = $data[$val]; } else { $data = null; break; } } return $data; } } ~~~ ## 3-2 模板引擎文件操作 >>模板引擎,將模板變量填充到模板文件中解析為Web界面。因此模板引擎可以對模板文件進行解析操作。 ### $template->display() >> 渲染模板內容, ~~~ public function display($content, $vars = [], $config = []) { if ($vars) { $this->data = $vars; } if ($config) { $this->config($config); } $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($content) . '.' . ltrim($this->config['cache_suffix'], '.'); if (!$this->checkCache($cacheFile)) { // 緩存無效 模板編譯 $this->compiler($content, $cacheFile); } // 讀取編譯存儲 $this->storage->read($cacheFile, $this->data); } ~~~ ### $template->fetch() >>渲染模板文件 ~~~ public function fetch($template, $vars = [], $config = []) { if ($vars) { $this->data = $vars; } if ($config) { $this->config($config); } if (!empty($this->config['cache_id']) && $this->config['display_cache']) { // 讀取渲染緩存 $cacheContent = Cache::get($this->config['cache_id']); if (false !== $cacheContent) { echo $cacheContent; return; } } $template = $this->parseTemplateFile($template); if ($template) { $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template) . '.' . ltrim($this->config['cache_suffix'], '.'); if (!$this->checkCache($cacheFile)) { // 緩存無效 重新模板編譯 $content = file_get_contents($template); $this->compiler($content, $cacheFile); } // 頁面緩存 ob_start(); ob_implicit_flush(0); // 讀取編譯存儲 $this->storage->read($cacheFile, $this->data); // 獲取并清空緩存 $content = ob_get_clean(); if (!empty($this->config['cache_id']) && $this->config['display_cache']) { // 緩存頁面輸出 Cache::set($this->config['cache_id'], $content, $this->config['cache_time']); } echo $content; } } ~~~ ## 3-3 模板引擎編譯緩存 >>模板引擎解析模板文件中根據配置信息,可以進行編譯緩存和渲染緩存 >編譯緩存(tpl_cahce),使用編譯存儲器(上面構造函數中創建的)存儲 >渲染緩存(display_cache),使用緩沖機制(Cache.php)存儲 ### $template->checkCache() >>檢查模板編譯緩存是否有效(tpl_cache) ~~~ private function checkCache($cacheFile) { // 未開啟緩存功能 if (!$this->config['tpl_cache']) { return false; } // 緩存文件不存在 if (!is_file($cacheFile)) { return false; } // 讀取緩存文件失敗 if (!$handle = @fopen($cacheFile, "r")) { return false; } // 讀取第一行 preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches); if (!isset($matches[1])) { return false; } $includeFile = unserialize($matches[1]); if (!is_array($includeFile)) { return false; } // 檢查模板文件是否有更新 foreach ($includeFile as $path => $time) { if (is_file($path) && filemtime($path) > $time) { // 模板文件如果有更新則緩存需要更新 return false; } } // 檢查編譯存儲是否有效 return $this->storage->check($cacheFile, $this->config['cache_time']); } ~~~ ### $template->isCache() >>檢查渲染緩存(display_cache)是否存在 ~~~ public function isCache($cacheId) { if ($cacheId && $this->config['display_cache']) { // 緩存頁面輸出 return Cache::has($cacheId); } return false; } ~~~ # 4 模板引擎解析輔助函數 ## 4-1 模板解析文件級操作 >> 模板引擎解析的輔助函數,在fetch/display接口中調用,用來解析模板文件的內容。 > 模板引擎解析模板文件時首先讀取模板文件內容 > 然后配合模板變量解析模板文件。 ### $template->parseTemplate() >>讀取模板文件內容 ~~~ private function parseTemplateFile($template) { if ('' == pathinfo($template, PATHINFO_EXTENSION)) { if (strpos($template, '@')) { // 跨模塊調用模板 $template = str_replace(['/', ':'], $this->config['view_depr'], $template); $template = APP_PATH . str_replace('@', '/' . basename($this->config['view_path']) . '/', $template); } else { $template = str_replace(['/', ':'], $this->config['view_depr'], $template); $template = $this->config['view_path'] . $template; } $template .= '.' . ltrim($this->config['view_suffix'], '.'); } if (is_file($template)) { // 記錄模板文件的更新時間 $this->includeFile[$template] = filemtime($template); return $template; } else { throw new TemplateNotFoundException('template not exists:' . $template, $template); } } ~~~ ### $template->compiler() >>編譯模板文件 ~~~ private function compiler(&$content, $cacheFile) { // 判斷是否啟用布局 if ($this->config['layout_on']) { if (false !== strpos($content, '{__NOLAYOUT__}')) { // 可以單獨定義不使用布局 $content = str_replace('{__NOLAYOUT__}', '', $content); } else { // 讀取布局模板 $layoutFile = $this->parseTemplateFile($this->config['layout_name']); if ($layoutFile) { // 替換布局的主體內容 $content = str_replace($this->config['layout_item'], $content, file_get_contents($layoutFile)); } } } else { $content = str_replace('{__NOLAYOUT__}', '', $content); } // 模板解析 $this->parse($content); if ($this->config['strip_space']) { /* 去除html空格與換行 */ $find = ['~>\s+<~', '~>(\s+\n|\r)~']; $replace = ['><', '>']; $content = preg_replace($find, $replace, $content); } // 優化生成的php代碼 $content = preg_replace('/\?>\s*<\?php\s(?!echo\b)/s', '', $content); // 模板過濾輸出 $replace = $this->config['tpl_replace_string']; $content = str_replace(array_keys($replace), array_values($replace), $content); // 添加安全代碼及模板引用記錄 $content = '<?php if (!defined(\'THINK_PATH\')) exit(); /*' . serialize($this->includeFile) . '*/ ?>' . "\n" . $content; // 編譯存儲 $this->storage->write($cacheFile, $content); $this->includeFile = []; return; } ~~~ >> 上面的模板編譯過程中 >首先 檢查是否啟用布局,讀取布局文件到模板文件中 >然后 解析讀取的模板文件內容。 >最后 優化模板編譯結果并保存 ### $template->parse() >> 解析模板內容 ~~~ public function parse(&$content) { // 內容為空不解析 if (empty($content)) { return; } // 替換literal標簽內容 $this->parseLiteral($content); // 解析繼承 $this->parseExtend($content); // 解析布局 $this->parseLayout($content); // 檢查include語法 $this->parseInclude($content); // 替換包含文件中literal標簽內容 $this->parseLiteral($content); // 檢查PHP語法 $this->parsePhp($content); // 獲取需要引入的標簽庫列表 // 標簽庫只需要定義一次,允許引入多個一次 // 一般放在文件的最前面 // 格式:<taglib name="html,mytag..." /> // 當TAGLIB_LOAD配置為true時才會進行檢測 if ($this->config['taglib_load']) { $tagLibs = $this->getIncludeTagLib($content); if (!empty($tagLibs)) { // 對導入的TagLib進行解析 foreach ($tagLibs as $tagLibName) { $this->parseTagLib($tagLibName, $content); } } } // 預先加載的標簽庫 無需在每個模板中使用taglib標簽加載 但必須使用標簽庫XML前綴 if ($this->config['taglib_pre_load']) { $tagLibs = explode(',', $this->config['taglib_pre_load']); foreach ($tagLibs as $tag) { $this->parseTagLib($tag, $content); } } // 內置標簽庫 無需使用taglib標簽導入就可以使用 并且不需使用標簽庫XML前綴 $tagLibs = explode(',', $this->config['taglib_build_in']); foreach ($tagLibs as $tag) { $this->parseTagLib($tag, $content, true); } // 解析普通模板標簽 {$tagName} $this->parseTag($content); // 還原被替換的Literal標簽 $this->parseLiteral($content, true); return; } ~~~ >>上面的模板解析過程中 > 首先 依次解析模板文件中出現的literal,extend,layout,include,php等特殊語法字符串。 >然后 調用標簽庫再次對其中的自定義標簽進行解析 >最后 解析普通模板變量標簽 >>這些模板內容的解析過程稱為語法塊級解析 ## 4-2模板解析語法塊級解析 ### $template->parseLiteral() >>literal語法解析 ~~~ private function parseLiteral(&$content, $restore = false) { $regex = $this->getRegex($restore ? 'restoreliteral' : 'literal'); if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) { if (!$restore) { $count = count($this->literal); // 替換literal標簽 foreach ($matches as $match) { $this->literal[] = substr($match[0], strlen($match[1]), -strlen($match[2])); $content = str_replace($match[0], "<!--###literal{$count}###-->", $content); $count++; } } else { // 還原literal標簽 foreach ($matches as $match) { $content = str_replace($match[0], $this->literal[$match[1]], $content); } // 清空literal記錄 $this->literal = []; } unset($matches); } return; } ~~~ ### $template->parseExtend() >>extend語法解析 ~~~ private function parseExtend(&$content) { $regex = $this->getRegex('extend'); $array = $blocks = $baseBlocks = []; $extend = ''; $func = function ($template) use (&$func, &$regex, &$array, &$extend, &$blocks, &$baseBlocks) { if (preg_match($regex, $template, $matches)) { if (!isset($array[$matches['name']])) { $array[$matches['name']] = 1; // 讀取繼承模板 $extend = $this->parseTemplateName($matches['name']); // 遞歸檢查繼承 $func($extend); // 取得block標簽內容 $blocks = array_merge($blocks, $this->parseBlock($template)); return; } } else { // 取得頂層模板block標簽內容 $baseBlocks = $this->parseBlock($template, true); if (empty($extend)) { // 無extend標簽但有block標簽的情況 $extend = $template; } } }; $func($content); if (!empty($extend)) { if ($baseBlocks) { $children = []; foreach ($baseBlocks as $name => $val) { $replace = $val['content']; if (!empty($children[$name])) { // 如果包含有子block標簽 foreach ($children[$name] as $key) { $replace = str_replace($baseBlocks[$key]['begin'] . $baseBlocks[$key]['content'] . $baseBlocks[$key]['end'], $blocks[$key]['content'], $replace); } } if (isset($blocks[$name])) { // 帶有{__block__}表示與所繼承模板的相應標簽合并,而不是覆蓋 $replace = str_replace(['{__BLOCK__}', '{__block__}'], $replace, $blocks[$name]['content']); if (!empty($val['parent'])) { // 如果不是最頂層的block標簽 $parent = $val['parent']; if (isset($blocks[$parent])) { $blocks[$parent]['content'] = str_replace($blocks[$name]['begin'] . $blocks[$name]['content'] . $blocks[$name]['end'], $replace, $blocks[$parent]['content']); } $blocks[$name]['content'] = $replace; $children[$parent][] = $name; continue; } } elseif (!empty($val['parent'])) { // 如果子標簽沒有被繼承則用原值 $children[$val['parent']][] = $name; $blocks[$name] = $val; } if (!$val['parent']) { // 替換模板中的頂級block標簽 $extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend); } } } $content = $extend; unset($blocks, $baseBlocks); } return; } ~~~ ### $template->parseLayout() >>layout語法解析 ~~~ private function parseInclude(&$content) { $regex = $this->getRegex('include'); $func = function ($template) use (&$func, &$regex, &$content) { if (preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $array = $this->parseAttr($match[0]); $file = $array['file']; unset($array['file']); // 分析模板文件名并讀取內容 $parseStr = $this->parseTemplateName($file); foreach ($array as $k => $v) { // 以$開頭字符串轉換成模板變量 if (0 === strpos($v, '$')) { $v = $this->get(substr($v, 1)); } $parseStr = str_replace('[' . $k . ']', $v, $parseStr); } $content = str_replace($match[0], $parseStr, $content); // 再次對包含文件進行模板分析 $func($parseStr); } unset($matches); } }; // 替換模板中的include標簽 $func($content); return; } ~~~ ### $template->parseInclude() >>include語法解析 ~~~ private function parseInclude(&$content) { $regex = $this->getRegex('include'); $func = function ($template) use (&$func, &$regex, &$content) { if (preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $array = $this->parseAttr($match[0]); $file = $array['file']; unset($array['file']); // 分析模板文件名并讀取內容 $parseStr = $this->parseTemplateName($file); foreach ($array as $k => $v) { // 以$開頭字符串轉換成模板變量 if (0 === strpos($v, '$')) { $v = $this->get(substr($v, 1)); } $parseStr = str_replace('[' . $k . ']', $v, $parseStr); } $content = str_replace($match[0], $parseStr, $content); // 再次對包含文件進行模板分析 $func($parseStr); } unset($matches); } }; // 替換模板中的include標簽 $func($content); return; } ~~~ ### $template->parsePhp() >>php語法解析 ~~~ private function parsePhp(&$content) { // 短標簽的情況要將<?標簽用echo方式輸出 否則無法正常輸出xml標識 $content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>' . "\n", $content); // PHP語法檢查 if ($this->config['tpl_deny_php'] && false !== strpos($content, '<?php')) { throw new Exception('not allow php tag', 11600); } return; } ~~~ ### $template->parseTagLib() >> 標簽庫解析,使用標簽庫解析模板內容,標簽庫的使用 見 模板標簽庫 章節 ~~~ public function parseTagLib($tagLib, &$content, $hide = false) { if (false !== strpos($tagLib, '\\')) { // 支持指定標簽庫的命名空間 $className = $tagLib; $tagLib = substr($tagLib, strrpos($tagLib, '\\') + 1); } else { $className = '\\think\\template\\taglib\\' . ucwords($tagLib); } $tLib = new $className($this); $tLib->parseTag($content, $hide ? '' : $tagLib); return; } ~~~ ### $template->parseTag() >> 簡單模板標簽解析 ~~~ private function parseTag(&$content) { $regex = $this->getRegex('tag'); if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $str = stripslashes($match[1]); $flag = substr($str, 0, 1); switch ($flag) { case '$': // 解析模板變量 格式 {$varName} // 是否帶有?號 if (false !== $pos = strpos($str, '?')) { $array = preg_split('/([!=]={1,2}|(?<!-)[><]={0,1})/', substr($str, 0, $pos), 2, PREG_SPLIT_DELIM_CAPTURE); $name = $array[0]; $this->parseVar($name); $this->parseVarFunction($name); $str = trim(substr($str, $pos + 1)); $this->parseVar($str); $first = substr($str, 0, 1); if (strpos($name, ')')) { // $name為對象或是自動識別,或者含有函數 if (isset($array[1])) { $this->parseVar($array[2]); $name .= $array[1] . $array[2]; } switch ($first) { case '?': $str = '<?php echo (' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>'; break; case '=': $str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>'; break; default: $str = '<?php echo ' . $name . '?' . $str . '; ?>'; } } else { if (isset($array[1])) { $this->parseVar($array[2]); $_name = ' && ' . $name . $array[1] . $array[2]; } else { $_name = ''; } // $name為數組 switch ($first) { case '?': // {$varname??'xxx'} $varname有定義則輸出$varname,否則輸出xxx $str = '<?php echo isset(' . $name . ')' . $_name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>'; break; case '=': // {$varname?='xxx'} $varname為真時才輸出xxx $str = '<?php if(!empty(' . $name . ')' . $_name . ') echo ' . substr($str, 1) . '; ?>'; break; case ':': // {$varname?:'xxx'} $varname為真時輸出$varname,否則輸出xxx $str = '<?php echo !empty(' . $name . ')' . $_name . '?' . $name . $str . '; ?>'; break; default: if (strpos($str, ':')) { // {$varname ? 'a' : 'b'} $varname為真時輸出a,否則輸出b $str = '<?php echo !empty(' . $name . ')' . $_name . '?' . $str . '; ?>'; } else { $str = '<?php echo ' . $_name . '?' . $str . '; ?>'; } } } } else { $this->parseVar($str); $this->parseVarFunction($str); $str = '<?php echo ' . $str . '; ?>'; } break; case ':': // 輸出某個函數的結果 $str = substr($str, 1); $this->parseVar($str); $str = '<?php echo ' . $str . '; ?>'; break; case '~': // 執行某個函數 $str = substr($str, 1); $this->parseVar($str); $str = '<?php ' . $str . '; ?>'; break; case '-': case '+': // 輸出計算 $this->parseVar($str); $str = '<?php echo ' . $str . '; ?>'; break; case '/': // 注釋標簽 $flag2 = substr($str, 1, 1); if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) { $str = ''; } break; default: // 未識別的標簽直接返回 $str = $this->config['tpl_begin'] . $str . $this->config['tpl_end']; break; } $content = str_replace($match[0], $str, $content); } unset($matches); } return; } ~~~ >>在上面的模板語法級解析過程中,還需要調用語法塊中的內容級解析 。 ## 4-3 模板解析內容級解析 ### $template->parseTemplateName() >>讀取繼承模板內容 ~~~ private function parseTemplateName($templateName) { $array = explode(',', $templateName); $parseStr = ''; foreach ($array as $templateName) { if (empty($templateName)) { continue; } if (0 === strpos($templateName, '$')) { //支持加載變量文件名 $templateName = $this->get(substr($templateName, 1)); } $template = $this->parseTemplateFile($templateName); if ($template) { // 獲取模板文件內容 $parseStr .= file_get_contents($template); } } return $parseStr; } ~~~ ### $template->parseBlock() >>extend中Block內容的解析 ~~~ private function parseBlock(&$content, $sort = false) { $regex = $this->getRegex('block'); $result = []; if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) { $right = $keys = []; foreach ($matches as $match) { if (empty($match['name'][0])) { if (count($right) > 0) { $tag = array_pop($right); $start = $tag['offset'] + strlen($tag['tag']); $length = $match[0][1] - $start; $result[$tag['name']] = [ 'begin' => $tag['tag'], 'content' => substr($content, $start, $length), 'end' => $match[0][0], 'parent' => count($right) ? end($right)['name'] : '', ]; $keys[$tag['name']] = $match[0][1]; } } else { // 標簽頭壓入棧 $right[] = [ 'name' => $match[2][0], 'offset' => $match[0][1], 'tag' => $match[0][0], ]; } } unset($right, $matches); if ($sort) { // 按block標簽結束符在模板中的位置排序 array_multisort($keys, $result); } } return $result; } ~~~ ### $template->parseAttr() >>解析標簽屬性 ~~~ public function parseAttr($str, $name = null) { $regex = '/\s+(?>(?P<name>[\w-]+)\s*)=(?>\s*)([\"\'])(?P<value>(?:(?!\\2).)*)\\2/is'; $array = []; if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $array[$match['name']] = $match['value']; } unset($matches); } if (!empty($name) && isset($array[$name])) { return $array[$name]; } else { return $array; } } ~~~ ### $template->getIncludeTagLib() >>搜索模板頁面中需要加載的標簽庫TagLib ~~~ private function getIncludeTagLib(&$content) { // 搜索是否有TagLib標簽 if (preg_match($this->getRegex('taglib'), $content, $matches)) { // 替換TagLib標簽 $content = str_replace($matches[0], '', $content); return explode(',', $matches['name']); } return null; } ~~~ ### $template->parseVar() >>解析模板變量 ~~~ public function parseVar(&$varStr) { $varStr = trim($varStr); if (preg_match_all('/\$[a-zA-Z_](?>\w*)(?:[:\.][0-9a-zA-Z_](?>\w*))+/', $varStr, $matches, PREG_OFFSET_CAPTURE)) { static $_varParseList = []; while ($matches[0]) { $match = array_pop($matches[0]); //如果已經解析過該變量字串,則直接返回變量值 if (isset($_varParseList[$match[0]])) { $parseStr = $_varParseList[$match[0]]; } else { if (strpos($match[0], '.')) { $vars = explode('.', $match[0]); $first = array_shift($vars); if ('$Think' == $first) { // 所有以Think.打頭的以特殊變量對待 無需模板賦值就可以輸出 $parseStr = $this->parseThinkVar($vars); } elseif ('$Request' == $first) { // 獲取Request請求對象參數 $method = array_shift($vars); if (!empty($vars)) { $params = implode('.', $vars); if ('true' != $params) { $params = '\'' . $params . '\''; } } else { $params = ''; } $parseStr = '\think\Request::instance()->' . $method . '(' . $params . ')'; } else { switch ($this->config['tpl_var_identify']) { case 'array': // 識別為數組 $parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']'; break; case 'obj': // 識別為對象 $parseStr = $first . '->' . implode('->', $vars); break; default: // 自動判斷數組或對象 $parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')'; } } } else { $parseStr = str_replace(':', '->', $match[0]); } $_varParseList[$match[0]] = $parseStr; } $varStr = substr_replace($varStr, $parseStr, $match[1], strlen($match[0])); } unset($matches); } return; } ~~~ ### $template->parseVarFunction() >>解析帶函數操作的模板變量 ~~~ public function parseVarFunction(&$varStr) { if (false == strpos($varStr, '|')) { return; } static $_varFunctionList = []; $_key = md5($varStr); //如果已經解析過該變量字串,則直接返回變量值 if (isset($_varFunctionList[$_key])) { $varStr = $_varFunctionList[$_key]; } else { $varArray = explode('|', $varStr); // 取得變量名稱 $name = array_shift($varArray); // 對變量使用函數 $length = count($varArray); // 取得模板禁止使用函數列表 $template_deny_funs = explode(',', $this->config['tpl_deny_func_list']); for ($i = 0; $i < $length; $i++) { $args = explode('=', $varArray[$i], 2); // 模板函數過濾 $fun = trim($args[0]); switch ($fun) { case 'default': // 特殊模板函數 if (false === strpos($name, '(')) { $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')'; } else { $name = '(' . $name . ' !== \'\'?' . $name . ':' . $args[1] . ')'; } break; default: // 通用模板函數 if (!in_array($fun, $template_deny_funs)) { if (isset($args[1])) { if (strstr($args[1], '###')) { $args[1] = str_replace('###', $name, $args[1]); $name = "$fun($args[1])"; } else { $name = "$fun($name,$args[1])"; } } else { if (!empty($args[0])) { $name = "$fun($name)"; } } } } } $_varFunctionList[$_key] = $name; $varStr = $name; } return; } ~~~ ### $template->parseThinkVar() >>解析Think模板變量 ~~~ public function parseThinkVar(&$vars) { $vars[0] = strtoupper(trim($vars[0])); $parseStr = ''; if (count($vars) >= 2) { $vars[1] = trim($vars[1]); switch ($vars[0]) { case 'SERVER': $parseStr = '$_SERVER[\'' . strtoupper($vars[1]) . '\']'; break; case 'GET': $parseStr = '$_GET[\'' . $vars[1] . '\']'; break; case 'POST': $parseStr = '$_POST[\'' . $vars[1] . '\']'; break; case 'COOKIE': if (isset($vars[2])) { $parseStr = '$_COOKIE[\'' . $vars[1] . '\'][\'' . $vars[2] . '\']'; } else { $parseStr = '\\think\\Cookie::get(\'' . $vars[1] . '\')'; } break; case 'SESSION': if (isset($vars[2])) { $parseStr = '$_SESSION[\'' . $vars[1] . '\'][\'' . $vars[2] . '\']'; } else { $parseStr = '\\think\\Session::get(\'' . $vars[1] . '\')'; } break; case 'ENV': $parseStr = '$_ENV[\'' . strtoupper($vars[1]) . '\']'; break; case 'REQUEST': $parseStr = '$_REQUEST[\'' . $vars[1] . '\']'; break; case 'CONST': $parseStr = strtoupper($vars[1]); break; case 'LANG': $parseStr = '\\think\\Lang::get(\'' . $vars[1] . '\')'; break; case 'CONFIG': if (isset($vars[2])) { $vars[1] .= '.' . $vars[2]; } $parseStr = '\\think\\Config::get(\'' . $vars[1] . '\')'; break; default: $parseStr = '\'\''; break; } } else { if (count($vars) == 1) { switch ($vars[0]) { case 'NOW': $parseStr = "date('Y-m-d g:i a',time())"; break; case 'VERSION': $parseStr = 'THINK_VERSION'; break; case 'LDELIM': $parseStr = '\'' . ltrim($this->config['tpl_begin'], '\\') . '\''; break; case 'RDELIM': $parseStr = '\'' . ltrim($this->config['tpl_end'], '\\') . '\''; break; default: if (defined($vars[0])) { $parseStr = $vars[0]; } } } } return $parseStr; } ~~~ ### $template->getRegex() >>獲取標簽語法的正則表達式 ~~~ private function getRegex($tagName) { $regex = ''; if ('tag' == $tagName) { $begin = $this->config['tpl_begin']; $end = $this->config['tpl_end']; if (strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1) { $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>[^' . $end . ']*))' . $end; } else { $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end; } } else { $begin = $this->config['taglib_begin']; $end = $this->config['taglib_end']; $single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false; switch ($tagName) { case 'block': if ($single) { $regex = $begin . '(?:' . $tagName . '\b(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>[^' . $end . ']*)|\/' . $tagName . ')' . $end; } else { $regex = $begin . '(?:' . $tagName . '\b(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end; } break; case 'literal': if ($single) { $regex = '(' . $begin . $tagName . '\b(?>[^' . $end . ']*)' . $end . ')'; $regex .= '(?:(?>[^' . $begin . ']*)(?>(?!' . $begin . '(?>' . $tagName . '\b[^' . $end . ']*|\/' . $tagName . ')' . $end . ')' . $begin . '[^' . $begin . ']*)*)'; $regex .= '(' . $begin . '\/' . $tagName . $end . ')'; } else { $regex = '(' . $begin . $tagName . '\b(?>(?:(?!' . $end . ').)*)' . $end . ')'; $regex .= '(?:(?>(?:(?!' . $begin . ').)*)(?>(?!' . $begin . '(?>' . $tagName . '\b(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end . ')' . $begin . '(?>(?:(?!' . $begin . ').)*))*)'; $regex .= '(' . $begin . '\/' . $tagName . $end . ')'; } break; case 'restoreliteral': $regex = '<!--###literal(\d+)###-->'; break; case 'include': $name = 'file'; case 'taglib': case 'layout': case 'extend': if (empty($name)) { $name = 'name'; } if ($single) { $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>[^' . $end . ']*)' . $end; } else { $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>(?:(?!' . $end . ').)*)' . $end; } break; } } return '/' . $regex . '/is'; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看