分享

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

 quasiceo 2015-06-09

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

浏览:259 发布日期:2015/06/05 分类:功能实现 关键字: include 动态引入 file template
动态引用 文件 include 支持变量解析
今天实例中用到了需要动态include文件 这样的方法,但thinkPHP不支持动态解析



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


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



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

再紧接着就是用户登录成功后,要调用不同的Level 也是不同的menu文件 ,如需要调用menu1.tpl 或 menu2.tpl 等。
但thinkPHP不支持动态引用,怎么办?
我个人的解决方案:
看图:
  1. <if condition="$Think.session.admin_user.level eq 1">
  2.     <include file="Public/menu"/>
  3. <else />
  4.     <include file="Public/menu" append="level" isVar="true"/>
  5. </if>
复制代码


说了,这里有对应着level 这个level是哪里来的呢? 是在controller中注入的
看图示:




为了给大家看的更明白,我把登录保存在session的数据,帖出来给大家看一下:免的不清楚是怎么回事


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






编辑:\ThinkPHP\Library\Think\Template.class.php 代码如下:
  1. protected function parseInclude($content, $extend = true){      
  2.         // 解析继承
  3.         if($extend)
  4.             $content    =   $this->parseExtend($content);
  5.         // 解析布局
  6.         $content    =   $this->parseLayout($content);
  7.         $begin      =   $this->config['taglib_begin'];
  8.         $end        =   $this->config['taglib_end'];
  9.         // 读取模板中的include标签
  10.         $find       =   preg_match_all('/'.$begin.'include\s(.+?)\s*?\/'.$end.'/is',$content,$matches);
  11.         if($find) {
  12.             for($i=0;$i<$find;$i++) {
  13.                 $include    =   $matches[1][$i];
  14.                 $array      =   $this->parseXmlAttrs($include);
  15.                 $file       =   $array['file'];
  16.                 //$view=Think::instance('Think\View');
  17.                 /*
  18.                 * 解析说明
  19.                 * 由于thinkPHP不能动态include文件
  20.                 * 因此这里需要做一些调整
  21.                 * 看看是否存在 append
  22.                 * **/
  23.                 if(isset($array['append'])){
  24.                     if(isset($array[isvar]) && $array[isvar]=="true"){
  25.                         $append=$this->get($array['append']);
  26.                         unset($array['isVar']);
  27.                     }else{
  28.                         $append=$array['append'];
  29.                     }
  30.                     $file=$file.$append;
  31.                     unset($array['append']);
  32.                 }
  33.                 if(isset($array['replace'])){
  34.                     if(isset($array[isVar]) && $array[isVar]=="true"){
  35.                         $file=$this->get($array['replace']);
  36.                         unset($array['isVar']);
  37.                     }else{
  38.                         $file=$array['replace'];
  39.                     }
  40.                     unset($array['replace']);
  41.                 }
  42.                 unset($array['file']);
  43.                 $content    =   str_replace($matches[0][$i],$this->parseIncludeItem($file,$array,$extend),$content);
  44.             }
  45.         }
  46.         return $content;
  47.     }
复制代码
需要的朋友拿走!!!!!!!!!!!
另一种实现方式:http://www./code/1261.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多