Home >php教程 >php手册 >TP模版布局Layout改进

TP模版布局Layout改进

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 11:35:481824browse

在使用Layout布局时,TP只提供了替换单一占位__CONTENT__布局。如果想要临时加点内容到特殊位置,那就得另外定义一个布局文件。本人在原基础上增加了占位符解析。可以方便在想要的位置插入代码
在使用Layout布局时,TP只提供了替换单一占位__CONTENT__布局。如果想要临时加点内容到特殊位置,那就得另外定义一个布局文件。本人在原基础上增加了占位符解析。可以方便在想要的位置插入代码。TP原布局方式仍然可用。具体代码如下//修改替换ThinkPHP/Library/Think/Template模版类中的parseLayout布局解析方法<br> //使用正则匹配placeholder区域,并替换placeholder特性name所指定的在布局中的占位<br> //占位格式:{__占位名__}<br> protected function parseLayout($content) {<br>         // 读取模板中的布局标签<br>         $find = preg_match('/'.$this->config['taglib_begin'].'layout\s(.+?)\s*?\/'.$this->config['taglib_end'].'/is',$content,$matches);<br>         if($find) {<br>             //替换Layout标签<br>             $content    =   str_replace($matches[0],'',$content);<br>             //解析Layout标签<br>             $array      =   $this->parseXmlAttrs($matches[1]);<br>             if(!C('LAYOUT_ON') || C('LAYOUT_NAME') !=$array['name'] ) {<br>                 // 读取布局模板<br>                 $layoutFile =   THEME_PATH.$array['name'].$this->config['template_suffix'];<br>                 $pattern='/'.$this->config['taglib_begin'].'placeholder\s([^'.$this->config['taglib_end'].']*?)'.$this->config['taglib_end'].'\s(.+?)\s*?'.$this->config['taglib_begin'].'\/placeholder'.$this->config['taglib_end'].'/is';<br>                $result= preg_match_all($pattern,$content,$matches);<br>                if($result){<br>                    $layout=file_get_contents($layoutFile);<br>                    foreach($matches[0] as $index=>$placeholder){<br>                        $array=$this->parseXmlAttrs($matches[1][$index]);<br>                        $replace='{__'.$array["name"]."__}";<br>                        $layout    =   str_replace($replace,$matches[2][$index],$layout);<br>                    }<br>                    $content=$layout;<br>                }else{<br>                    $replace    =   isset($array['replace'])?$array['replace']:$this->config['layout_item'];<br>                    // 替换布局的主体内容<br>                    $content    =   str_replace($replace,$content,file_get_contents($layoutFile));<br>                }<br>                 //替换掉模版中空的占位<br>                 $content=preg_replace('/{__\w+?__}/','',$content);<br>             }<br>         }else{<br>             $content = str_replace('{__NOLAYOUT__}','',$content);<br>         }<br> <br>         return $content;<br>     }使用实例:
布局文件:<br> {__HEADER__}<br> {__CONTENT__}<br> {__FOOTER__}<br> 应用:<layout></layout><br> <placeholder></placeholder><br> <placeholder></placeholder><br> <placeholder></placeholder>这样,就可以在你的布局文件中定义任意的占位符,方便你在想要的位置插入代码

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn