thinkphp Widget Extension Instance _php instance

Source: Internet
Author: User

The thinkphp widget extension is used to output different content based on the page needs, and it is defined in Lib/widget in the project directory.

The specific definition is as follows:

Class Newslistwidget extends widget{public
  function render ($data) {
    //code ...
  }
}

Need to note:

1.Widget is an abstract class in which there is an abstract method (abstract) render, which must be implemented in subclasses;
The Render method of 2.Widget must be returned using return instead of direct output;
3. $data is the parameter of the incoming widget.

We can then call this widget directly in the template:

{: W (' newslist ', Array (' Tmpl ' => ')}

Here I passed in a parameter, which is a more common usage, what is the widget used for? Depending on the page needs to output different content, this different content, can be different from the data, of course, can be different templates.

Class Newslistwidget extends widget{public
  function render ($data) {
    //code
    $news; This can be a data retrieval statement that retrieves a dataset
    $html = $this->renderfile ($data [' Tmpl '], $news);
    return $html;
  }


This will automatically render the contents of the template file/lib/widget/newslist/a.html and send the $news to the past, which can be processed as a Normal template file and then output.

Of course, can also be in the action controller to get the contents of the widget, two times processing.

$content = W (' newslist ', Array (' Tmpl ' => '), TRUE); The third parameter indicates whether the string is returned, and the default is False, which represents the direct output.

In addition, thinkphp is the MVC framework, please put the data retrieval related content on the model layer

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.