作者:有有1988_540 | 来源:互联网 | 2023-08-14 18:14
tp5框架下:thinkphp\library\think\Template.php
原方法:
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);}};$func($content);return;}
更新后:
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']);if (0 === strpos($file, '$')) {if(strpos($file,'/')!==false){$val= $this->get(substr($file, 1,strpos($file,'/')-1));$file =$val.substr($file, strpos($file,'/'));}else{$file = $this->get(substr($file, 1));}}$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);}};$func($content);return;}
主要增加了一段解析变量的代码
if (0 === strpos($file, '$')) {if(strpos($file,'/')!==false){$val= $this->get(substr($file, 1,strpos($file,'/')-1));$file =$val.substr($file, strpos($file,'/'));}else{$file = $this->get(substr($file, 1));}
}
如果引用的文件中使用了自定义标签
更新前在html中这样写会解析不了变量$PATH(控制器中定义的)
{include file="$PATH/tel/head.html" /}
更新后可以正常解析