Home  >  Article  >  php教程  >  ThinkPHP 模版中动态 include文件 支持变量解析

ThinkPHP 模版中动态 include文件 支持变量解析

WBOY
WBOYOriginal
2016-06-07 11:38:331325browse

动态引用 文件 include 支持变量解析
今天实例中用到了需要动态include文件 这样的方法,但thinkPHP不支持动态解析



使用场景: 用户登录后,根据等级调用不同的显示页面,因此需要动态引入文件,而thinkPHP恰恰又不支持动态引入,只好自己想办法了。


那应该怎么去使用呢?
如图所示:

ThinkPHP 模版中动态 include文件 支持变量解析

以上就是数据库对应着文件。

再紧接着就是用户登录成功后,要调用不同的Level 也是不同的menu文件 ,如需要调用menu1.tpl 或 menu2.tpl 等。
但thinkPHP不支持动态引用,怎么办?
我个人的解决方案:
看图:<if><br>     <include></include><br> <else></else><br>     <include></include><br> </if>ThinkPHP 模版中动态 include文件 支持变量解析

说了,这里有对应着level 这个level是哪里来的呢? 是在controller中注入的
看图示:
ThinkPHP 模版中动态 include文件 支持变量解析

ThinkPHP 模版中动态 include文件 支持变量解析

为了给大家看的更明白,我把登录保存在session的数据,帖出来给大家看一下:免的不清楚是怎么回事
ThinkPHP 模版中动态 include文件 支持变量解析

需要修改 \ThinkPHP\Library\Think\Template.class.php 对应着parseInclude() 方法:具体修改如图所示:

ThinkPHP 模版中动态 include文件 支持变量解析

ThinkPHP 模版中动态 include文件 支持变量解析


编辑:\ThinkPHP\Library\Think\Template.class.php 代码如下:protected function parseInclude($content, $extend = true){      <br>         // 解析继承<br>         if($extend)<br>             $content    =   $this->parseExtend($content);<br>         // 解析布局<br>         $content    =   $this->parseLayout($content);<br>         $begin      =   $this->config['taglib_begin'];<br>         $end        =   $this->config['taglib_end'];<br>         // 读取模板中的include标签<br>         $find       =   preg_match_all('/'.$begin.'include\s(.+?)\s*?\/'.$end.'/is',$content,$matches);<br>         if($find) {<br>             for($i=0;$i                 $include    =   $matches[1][$i];<br>                 $array      =   $this->parseXmlAttrs($include);<br>                 $file       =   $array['file'];<br>                 //$view=Think::instance('Think\View');<br>                 /*<br>                 * 解析说明<br>                 * 由于thinkPHP不能动态include文件<br>                 * 因此这里需要做一些调整<br>                 * 看看是否存在 append<br>                 * **/<br>                 if(isset($array['append'])){<br>                     if(isset($array[isvar]) && $array[isvar]=="true"){<br>                         $append=$this->get($array['append']);<br>                         unset($array['isVar']);<br>                     }else{<br>                         $append=$array['append'];<br>                     }<br>                     $file=$file.$append;<br>                     unset($array['append']);<br>                 }<br>                 if(isset($array['replace'])){<br>                     if(isset($array[isVar]) && $array[isVar]=="true"){<br>                         $file=$this->get($array['replace']);<br>                         unset($array['isVar']);<br>                     }else{<br>                         $file=$array['replace'];<br>                     }<br>                     unset($array['replace']);<br>                 }<br>                 unset($array['file']);<br>                 $content    =   str_replace($matches[0][$i],$this->parseIncludeItem($file,$array,$extend),$content);<br>             }<br>         }<br>         return $content;<br>     }需要的朋友拿走!!!!!!!!!!!



另一种实现方式:http://www.thinkphp.cn/code/1261.html

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