Home > PHP Framework > ThinkPHP > How to use the fetch method in thinkphp

How to use the fetch method in thinkphp

WBOY
Release: 2022-06-08 17:14:40
Original
4799 people have browsed it

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);".

How to use the fetch method in thinkphp

The operating environment of this article: Windows 10 system, ThinkPHP version 6, Dell G3 computer.

How to use the fetch method in thinkphp

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();
Copy after login

If your controller inherits the \think\Controller class, you can use it directly

 // 渲染模板输出
return $this->fetch();
Copy after login

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();
Copy after login

, 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).html

If (specified operation) calls:

return $view->fetch('add');
Copy after login

, then the positioning template file is:

>[ info]

[Template file directory]/current controller name/add.html

If a template file calling the controller uses:

return $view->fetch('user/add');
Copy after login

, 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');
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template