Widget extension in ThinkPHP

Source: Internet
Author: User
Widget extension is used to output different content according to the page requirements. it is defined under Lib/Widget in the project directory. Definition: classNewsListWidgetextendsWidget {publicfunctionrender...

The Widget extension is used to output different content based on the page requirements. it is defined under Lib/Widget in the project directory.

Definition:

  1. Class NewsListWidget extends Widget {
  2. Public function render ($ data ){
  3. // Code...
  4. }
  5. }

Note:

1) Widget is an abstract class with an abstract render that must be implemented in the subclass;

2) the render method of the Widget must be returned using return instead of output directly;

3) $ data is the input Widget parameter.

Then we can directly call this Widget in the template.

{: W ('newlist', 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 different data, or different templates.

  1. Class NewsListWidget extends Widget {
  2. Public function render ($ data ){
  3. // Code
  4. $ News; // A dataset can be retrieved using a data retrieval statement.
  5. $ Html = $ this-> renderFile ($ data ['tmpl'], $ news );
  6. Return $ html;
  7. }
  8. }

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.