Brief introduction of three ways of rendering templates in thinkPHP5 framework

  • 2021-08-16 23:16:44
  • OfStack

This paper illustrates three ways of rendering templates in thinkPHP5 framework. Share it for your reference, as follows:

By default, the output of the controller adopts return mode, without any manual output, and the system will automatically complete the output of rendered content.

Render the template in the controller


namespace app\index\controller;
use think\view;
class Index{
 public function index(){
  $view = new view();
  return $view->fetch('index');
 }
}

Use the view helper function directly to render the template


namespace app\index\controller;
class Index{
 public function index() {
  return view('index');
 }
}

Inherit the think\ Controller class

If you inherit the think\ Controller class, you can call the methods of the think\ View and think\ Request classes directly. Examples:


namespace app\index\controller;
use think\Controller;
class Index extends Controller{
 public function index(){
  $this->assign('domain', $this->request->url(true));
  return $this->fetch('index');
 }
}

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

Hope that this article is based on the framework of ThinkPHP PHP programming help.


Related articles: