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