thinkphp Widget Extension Instance _php tutorial

Source: Internet
Author: User
The thinkphp widget extension is used to output different content according to the needs of the page, which is defined under Lib/widget in the project directory.

It is defined 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 of render, which must be implemented in subclasses;
The 2.Widget render method must return using return instead of the direct output;
3. $data is the parameter of the incoming widget.

We can then call the widget directly in the template:

{: W (' newslist ', Array (' tmpl ' = ' A ')}

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

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

This time will automatically render the template file/lib/widget/newslist/a.html content, and transfer the $news past, can be used when the Normal template file processing, and then output.

Of course, the contents of the widget can be obtained within the action controller and processed two times.

$content = W (' newslist ', Array (' tmpl ' = ' a '), TRUE); The third parameter indicates whether a 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 in the model layer

http://www.bkjia.com/PHPjc/825367.html www.bkjia.com true http://www.bkjia.com/PHPjc/825367.html techarticle the thinkphp widget extension is used to output different content according to the needs of the page, which is defined under Lib/widget in the project directory. Specifically defined as follows: Class Newslistwidget extends widget{public ...

  • 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.