Home >Backend Development >PHP Tutorial >Two ways to write and call Widget extensions in ThinkPHP
This article mainly introduces the two ways of writing and calling Widget extensions in ThinkPHP, and analyzes in detail the writing methods and corresponding calling techniques of Widget extensions. Friends in need can refer to the following
General use of Widget extensions Due to the expansion of page components, different content can be output on the page as needed. Here are two ways of writing and calling Widgets in ThinkPHP
Writing method one:
ArticlWidget.class.php file:
class ArticleWidget extends Widget { /** * * @param array $data * @return type * 调用方法:{:W('ArticleList',array('cid'=>25,'limit'=>5))} * cid为分类id,limit为调用数量 */ 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>
Writing method two:
class ArticleWidget extends Action { /** * * @param array $data * @return type * 调用方法:{:R('Article/articleList',array('cid'=>25,'limit'=>5),'Widget')} * cid为分类id,limit为调用数量 */ 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, the content is the same as the writing method, but it is placed in the Tpl/style name/Widget/ directory
If the template file is placed in the Article folder of the directory where the ArticleWiget.class.php file is located, the writing method is as follows :
$this->display(dirname(__FILE__) . '/Article/articleList.html');
PHP9 predefined variables Detailed explanation of super global array usage_php basics
PHP Random number C extended random number
Understanding php dependency injection and inversion of control_php skills
The above is the detailed content of Two ways to write and call Widget extensions in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!