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