ThinkPHP5 的视图$view->fetch()和$view->display()的区别
ThinkPHP5 的视图$view->fetch()和$view->display()的区别,文档写得很简单,很多人不是很明白。
ThinkPHP5 的视图$view->fetch()和$view->display()的区别,文档写得很简单,很多人不是很明白。
方法 | 说明 |
---|---|
fetch | 渲染模板输出 |
display | 渲染内容输出 |
看看他们的区别:
fetch()
是用模板文件来输出:
如果 fetch()
不传参数,程序会自动寻找 index/index.html
渲染输出。
class Index extends BaseController{ /** * 首页 * @return mixed */ public function index() { return $this->fetch(); }}
如果传参数,比如我们用 hello.html
来渲染
/** * 首页 * @return mixed */ public function index() { return $this->fetch('hello'); }
而如果是
display()
,则直接输出传递的内容:
由于没传参数,display()
页面会渲染出Layout,但不会有任何内容。
class Index extends BaseController{ /** * 首页 * @return mixed */ public function index() { return $this->display(); }}
如果传递参数, 比如 "hello"
,那么页面会直接输出字符串 "hello"
/** * 首页 * @return mixed */ public function index() { return $this->display("hello"); }