This article mainly introduces the two methods of writing and invoking the widget extension in thinkphp, analyzes the method of the widget extension and the corresponding calling skill, and the friend who needs to refer to the following
Widget extensions are generally used to extend the page components, and to output different content on the page as needed, here's a look at the two ways in which widgets are written and called in thinkphp
Writing one:
ArticlWidget.class.php file:
Class Articlewidget extends Widget { /** * * @param array $data * @return Type * Call method: {: W (' Articlelist ', Array (' CID ' =>25, ' limit ' =>5)} * CID for classification Id,limit for number of calls * /Public Function render ($ Data) { $Article = M (' article '); $articleMap ["cid"] = $data ["CID"]; $data ["articlelist"] = $Article->where ($articleMap)->order (' id desc ')->limit ($data ["Limit"])->select ( ); foreach ($articleList as $key = + $value) { if ($value ["thumbnail"] = = "") { $data ["Articlelist"] [$key] [" Thumbnail "] = '/public/img/common/nothumbnail.jpg '; } } return $this->renderfile (' articlelist ', $data); }}
Template file articlelist.html in lib/widget/article directory
<volist name= "articlelist" id= "Articlelist_vo" > <li> <a href= "__app__/channel/ articledetail/code/article/id/{$articleList _vo.id} "rel=" external nofollow "title=" {$articleList _vo.title} ">{$ Articlelist_vo.title}</a> </li></volist>
Two:
Class Articlewidget extends Action { /** * * @param array $data * @return Type * Call method: {: R (' article/ Articlelist ', Array (' CID ' =>25, ' limit ' =>5), ' Widget ')} * CID for classification Id,limit for number of calls */public function Articlelist ($cid, $limit) { $Article = M (' article '); $articleMap ["cid"] = $cid; $data = $Article->where ($articleMap)->order (' id desc ')->limit ($limit)->select (); foreach ($data as $key + $value) { if ($value ["thumbnail"] = = "") { $data [$key] ["thumbnail"] = '/public/img/ Common/nothumbnail.jpg '; } } $this->assign (' articlelist ', $data); $this->display (' widget:articlelist ');} }
Template file articlelist.html, content same as one, but placed in tpl/style name/widget/directory
If the template file is placed under the article folder in the directory where the ArticleWiget.class.php file is located, the following is the wording:
$this->display (DirName (__file__). '/article/articlelist.html ');
Related recommendations:
PHP pre-defined variables 9 large Super Global array usage detailed _php basics
PHP Random number C Extended random number
Understanding PHP Dependency Injection and control reversal _php tips