include 的扩展 (二)修改parseInclude函数

浏览:538 发布日期:2016/02/26 分类:技术分享
模板解析 Tag include 函数位于 ThinkPHP/Library/Think/Template.class.php
中的parseInclude 函数
我修改了     // 解析模板中的include标签
    protected function parseInclude($content, $extend = true){      
        // 解析继承
        if($extend)
            $content    =   $this->parseExtend($content);
        // 解析布局
        $content    =   $this->parseLayout($content);
        $begin      =   $this->config['taglib_begin'];
        $end        =   $this->config['taglib_end'];
        // 读取模板中的include标签
        $find       =   preg_match_all('/'.$begin.'include\s(.+?)\s*?\/'.$end.'/is',$content,$matches);
        if($find) {
            for($i=0;$i<$find;$i++) {
                $include    =   $matches[1][$i];
                $array      =   $this->parseXmlAttrs($include);
                $file       =   $array['file'];
            //使用方法 
           //<include file="common/top" skin="skinname" isvar="true" /> 
           // skinname可以只是一个字符串 后面isvar 是表示 true的话 作为一个变量来执行 当前view页面从controller 获取的变量
              if(isset($array['skin'])){
                    if(isset($array[isvar]) && $array[isvar]=="true"){
                        $skin=$this->get($array['skin']);
                        unset($array['isVar']);
                    }else{
                        $skin=$array['skin'];
                    }
                    $file= $skin."/".$file;
                    unset($array['skin']);
                }
            //路径为 skinname /common/top.html 至于 你可以做其他修改 
             
                unset($array['file']);
                $content    =   str_replace($matches[0][$i],$this->parseIncludeItem($file,$array,$extend),$content);
            }
        }
        return $content;
    }
上面自己看下 实现了功能
最佳答案
评论( 相关
后面还有条评论,点击查看>>