function and usage analysis of widgets in THINKPHP5 framework

Source: Internet
Author: User
This article mainly introduces the function and usage of widgets in thinkPHP5 framework, and analyzes the related concepts, usage methods and operation notes of widgets in ThinkPHP5, and the need of friends can refer to the following

This article describes the function and usage of widgets in the THINKPHP5 framework. Share to everyone for your reference, as follows:

Note: when rendered with the helper function return view() , the pendant function fails and you must use thereturn $this->fetch()

When we use the template, the general site of the top (such as the navigation bar, or user login area, etc.), as well as the end of the site footer (such as links or copyright instructions, etc.), and the body area, we will simplify the code, will be the three pieces of separation, It is then used in a way that inherits from the template.

But if the top and the tail are just plain HTML code good to say, but there is usually a variable assignment and loop traversal and judgment and other logic.

Of course, the original method is that we use native PHP code in these two areas to achieve, but this does not conform to the concept of THINKPHP5 framework, so in the early version of the widget, familiar with WordPress and Yii framework are very familiar, this is built pendant, Widget for something.

He can be very convenient for you to set up a locale containing variables such as variable loop output, such as advertising module, Calendar module, and so on.

Here's how to simply use this feature in THINKPHP5.

First we need in the module directory, resume widget directory, and then create a blog controller inside, and then in the index method to write the following code:

Note that the book in the code is a custom model for me, just to work with examples.

Of course you can inherit the controller, or you can choose not to inherit, depending on your use of the framework of the habit, of course, if you do not inherit, you need to use the View Assistant rendering template.

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

Focus: $this->fetch (); don't think it's not written. The template path defaults to the index view in the blog folder of the View view.

In the widget here is not possible, must fill in the corresponding view path

The corresponding view path is index.html under the blog directory under View view, of course I you can customize the view name

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, just as an example to use

Here 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-world application, such as a navigation bar traversal output, or many many pendant modules

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

{:widget('Blog/index')}This way to output, the blog is the widget directory of the controller, index is the method name in the controller

<!doctype html>

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

The default home page output shown here

If the widget is working properly, the following output iterates through the contents of a data table

1+weilai

2+yanyan

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.