In thinkphp, the fetch method is used to render template file output. This method does not render the output directly, but supports tag parsing of the template or content. It returns the parsed content and the rendering output system will also automatically call it. The send method performs rendering output, and the syntax is "$this->fetch(template name);".
The operating environment of this article: Windows 10 system, ThinkPHP version 6, Dell G3 computer.
Instancing the view class
5.0 template rendering provides two methods, fetch and display, the most commonly used ones are The fetch
fetch method is used to render template file output, while the
display method is used to render content output.
// 实例化视图类 $view = new \think\View(); // 渲染模板输出 return $view->fetch();
If your controller inherits the \think\Controller class, you can use it directly
// 渲染模板输出 return $this->fetch();
It should be noted that the view fetch method of ThinkPHP5 will not directly render the output, but just return Parsed content. If the view parsed content is returned in the controller class, the rendering output system will automatically call the send method of the think\Response class for rendering output.
Template positioning rules
The template file directory is located under the view directory of the module by default. The positioning rules of the template file in the fetch method of the view class are as follows:
If the fetch method without any parameters is called:
return $view->fetch();
, the template file will be located according to the system's default rules:
>[info]
[template file directory]/current Controller name (lowercase)/current operation name (lowercase).htmlIf (specified operation) calls:return $view->fetch('add');
[Template file directory]/current controller name/add.html
If a template file calling the controller uses:
return $view->fetch('user/add');
, then the positioning template file is:
[Template file directory]/user/add.html
Full path template call:
return $view->fetch(MODULE_PATH.'view/public/header.html');
Then locate the template file as:
MODULE_PATH.'view /public/header.html'Recommended learning: "
PHP Video Tutorial
The above is the detailed content of How to use the fetch method in thinkphp. For more information, please follow other related articles on the PHP Chinese website!