dd

获取模板继承中指定block块的HTML编译代码

jerry thinkphp 2015年11月19日 收藏
用于AJAX 获取 模板中需要变动的部分代码
可写在公共继承类,例如AdminBaseController.class.php 用于覆盖 Controller的display() 方法
    final protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix=''){
        if(empty($templateFile)) $templateFile=ACTION_NAME;
        if(IS_AJAX){
            C('SHOW_PAGE_TRACE',false);
            //获取指定继承块模板
            $block=I('block','');
            if(!empty($block)){
                //获取模板文件路径
                $tpl_file=$this->view->parseTemplate($templateFile);
                //获取模板文件内容
                $tpl_content=file_get_contents($tpl_file);
                //匹配指定block内容
                $tpl_find=preg_match('/<block\sname=[\'"]'.$block.'[\'"]\s*?>(.*?)<\/block>/is',$tpl_content,$tpl_block);
                if($tpl_find){
                    //编译指定block内容
                    $tpl_html=$this->view->fetch($templateFile,$tpl_block[1]);
                    $this->ajaxReturn($tpl_html);
                }
            }

            $content = $this->view->fetch($templateFile);
            $this->ajaxReturn($content);
        }else{
            $this->view->display($templateFile);
        }
    }
BY:悠悠山雨
dd