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.