Detailed Explanation of the Function and Usage of widget in the Framework of thinkPHP5

  • 2021-10-15 09:57:35
  • OfStack

This paper illustrates the function and usage of widget in thinkPHP5 framework. Share it for your reference, as follows:

Note: Using helper functions return view() Render, the pendant function will fail and you must use return $this->fetch()

When we use templates, the top of a website (such as navigation bar, or user login area, etc.), and the tail of the website footer (such as friendship links or copyright descriptions, etc.) and body areas, we will cut these three parts apart in order to simplify the code, and then use them through template inheritance.

However, if the top and tail are just simple html code, it is easy to say, but there are usually variables assignment, loop traversal and judgment logic.

Of course, the original method is that we use the native php code in these two areas, but this does not conform to the concept of thinkphp5 framework, so there is widget in the early version, and those who are familiar with wordpress and yii frameworks are very familiar with it. These are all things that build pendants and widgets.

It is very convenient for you to set the output including variable loop in a certain area of the website, such as advertisement module, calendar module, etc.

The following describes how to use this function simply in thinkphp5.

First of all, we need to resume the widget directory under the module directory, then establish the Blog controller in it, and then write the following code in the index method:

Note that Book in the code is my custom model, which is only used with examples.

Of course, you can inherit Controller or you can choose not to, depending on your usage of the framework. Of course, if you don't, you need to use the view helper to render the template.

The code means that the data in the model Book is taken out and assigned to the template.

Highlights: $this- > fetch (); Don't think that if you don't write the template path, you will only want the index view in the blog folder under the view view by default.

This is not possible in widget. You must fill in the corresponding view path

The corresponding view path is index. html in blog directory under view view. Of course, you can customize the view name for i


namespace app\index\widget;
use app\index\model\Book;
use think\Controller;
class Blog extends Controller
{
  public function index()
  {
    $list = Book::all();
    $this->assign('list',$list);
    return $this->fetch('widget/index');
  }
}

Then the code in view/blog/index. html is as follows, for example only

Where the custom output traverses the specified content


{volist name="list" id="v"}
<p>{$v.id}+{$v.bookname}</p>
{/volist}

You can use any function you want in a real application, such as navigation bar traversal output, or many, many pendant modules

How to call it, you can use the following in your specified view template or in the inherited public template

{:widget('Blog/index')} Output in this way, Blog is the controller under widget directory, and index is the method name in the controller


<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
     content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <p> The default home page output shown here </p>
  <p> If widget Use normally , The traversal will be output below 1 Contents of data tables </p>
  {:widget('Blog/index')}
</body>
</html>

At this point, complete the output of the following page!

The default home page output shown here

If widget works properly, the following output will traverse the contents of 1 data table

1+weilai

2+yanyan

For more readers interested in thinkPHP related contents, please 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 ThinkPHP framework of PHP programming help.


Related articles: