Two Methods of writing and calling the Widget extension in ThinkPHP, thinkphpwidget
This article describes two methods of writing and calling widgets extension in ThinkPHP. We will share this with you for your reference. The details are as follows:
Widget extensions are generally used for page component extensions. Different content is output on the page as needed. The following describes two methods of Widget writing and calling in ThinkPHP.
Statement 1:
ArticlWidget. class. php file:
Class ArticleWidget extends Widget {/***** @ param array $ data * @ return type * Call method: {: W ('articlelist', array ('cid '=> 25, 'limit' => 5)} * cid is the category id, and limit is the 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 );}}
The template file articlelist.html is in the 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>
Statement 2:
Class ArticleWidget extends Action {/***** @ param array $ data * @ return type * Call method: {: R ('Article/articlelist ', array ('cid' => 25, 'limit '=> 5), 'components')} * cid is the category id, limit is the 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, which is written in the same way, but stored in the Tpl/style name/Widget/directory
If the template file is placed in the Article folder in the directory where the articlewiet. class. php file is located, the syntax is as follows:
$this->display(dirname(__FILE__) . '/Article/articleList.html');