Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the fetch method in thinkphp

Shulou Source: shulou.com Published: 2022-05-31 21:07:00 09月21日 Update

This article mainly introduces "how to use the fetch method in thinkphp". In daily operation, I believe many people have doubts about how to use the fetch method in thinkphp. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts about "how to use the fetch method in thinkphp"! Next, please follow the small series to learn together!

In thinkphp, fetch method is used to render template file output. This method does not directly render output, but supports label parsing of template or content, and returns parsed content. The rendering output system will also automatically call send method to render output. The syntax is "$this->fetch(template name);".

Operating environment: Windows 10 system, ThinkPHP3.2 version, Dell G3 computer.

How to use fetch method in thinkphp

instantiate view class

5.0 Template rendering provides fetch and display methods, the most commonly used is fetch

fetch method is used to render template file output, while

The display method renders content output.

//instantiate view class $view = new \think\View(); //render template output return $view->fetch();

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

//render template output return $this->fetch();

Note that ThinkPHP5's view fetch method doesn't render output directly, it just returns parsed content. If the controller class returns the view parse content, the render output system automatically calls the send method of the think\Response class to render the output.

template positioning rule

By default, the template file directory is located under the view directory of the module. The template file positioning rules in the fetch method of the view class are as follows:

If the fetch method is called without any arguments:

return $view->fetch();

Then locate the template file according to the default rules of the system to:

>[info] ### [template file directory]/current controller name (lowercase)/current operation name (lowercase).html

If (specify action) calls:

return $view->fetch('add');

The location template file is:

>[info] ### [template file directory]/current controller name/add.html

If one of the template files calling the controller uses:

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

Then the location template file is:

The requested URL/user/add.html was not found on this server.

Full Path Template Call:

return $view->fetch(MODULE_PATH. 'view/public/header.html');

Then the location template file is:

MODULE_PATH. 'view/public/header.html'

At this point, the study of "how to use fetch method in thinkphp" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Templates methods files output positioning content controllers directories views controls systems learning rules instances lowercase more help practical next two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Huawei vpn Shulou Technology