ThinkPHP Widget extension instance _ php instance

Source: Internet
Author: User
This article mainly introduces ThinkPHP Widget extensions. If you need ThinkPHP widgets, you can refer to ThinkPHP Widget extensions to output different content based on page requirements. They are defined under Lib/widgets in the project directory.

The specific definition is as follows:

class NewsListWidget extends Widget{  public function render($data){    // code...  }}

Note:

1. Widget is an abstract class with an abstract render which must be implemented in the subclass;
2. The render method of the Widget must be returned using return instead of directly outputting it;
3. $ data is the input Widget parameter.

Then we can directly call This Widget in the template:

{:W('NewsList', array('tmpl' => 'a'))}

Here I passed in a parameter. This is a common usage. What is the Widget used? Different content is output based on the page requirements. Different content can be data or template.

Class NewsListWidget extends Widget {public function render ($ data) {// code $ news; // here is a data retrieval statement to retrieve a dataset $ html = $ this-> renderFile ($ data ['tmpl'], $ news); return $ html ;}}

At this time, the template file/Lib/Widget/NewsList/a.html will be automatically rendered, and $ news will be transferred to the past. It can be processed as a normal template file and then output.

Of course, you can also obtain the Widget content in the Action controller for secondary processing.

$ Content = W ('newlist', array ('tmpl' => 'A'), TRUE); // The third parameter indicates whether a string is returned. The default value is FALSE, indicates direct output.

In addition, ThinkPHP is a MVC Framework. Please put the content related to data retrieval 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.