Home  >  Article  >  Backend Development  >  thinkphp中怎么让assign在另一个模板里使用呢?

thinkphp中怎么让assign在另一个模板里使用呢?

WBOY
WBOYOriginal
2016-06-23 13:12:151305browse

比如现在我有a.html和名字为b的控制器,现在我在b控制器里assing('b',$b),那么请问在a.html中怎么调用b控制器中的assign呢?


回复讨论(解决方案)

代码是这样的
Controller:
public function a(){          
            $this->display();
}
    
public function b(){
        if(IS_AJAX){
            $image=D('Image');
            $b=$image->getSelectImg(I('post.tid'));           
            $this->assign('b',$b);
        }    
    }


a.html:

public function a(){     
            $this->b();     
            $this->display();
}
    

直接在 a 方法里面调用一下 b 方法

直接在a.html中写

{$b}

这是不可以的!
a 方法显示 a.html 模版内容是无条件的
b 方法中 $this->assign('b',$b); 的执行是有条件的:在 ajax 方式下执行
并且查询数据的条件是传入的,即 a 方法中 I('post.tid') 多半是无值的

如果你只是想复用那点查询赋值代码的话,需要另写一个带参数的方法 c,分别在 a、b 中调用

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