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:
- Class NewsListWidget extends Widget {
- Public function render ($ data ){
- // Code...
- }
- }
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.
- Class NewsListWidget extends Widget {
- Public function render ($ data ){
- // Code
- $ News; // A dataset can be retrieved using a data retrieval statement.
- $ 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.