search
Homephp教程php手册TP模版布局Layout改进

在使用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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft