这篇文章主要介绍了ThinkPHP中Widget扩展如何调用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
写法一:
ArticlWidget.class.php文件:
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); } }
模板文件articleList.html在Lib/Widget/Article目录下
<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>
写法二:
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'); } }
模板文件articleList.html,内容同写法一,但放在Tpl/风格名/Widget/目录下
如果模板文件放在ArticleWiget.class.php文件所在目录的Article文件夹下,则写法如下:
$this->display(dirname(__FILE__) . '/Article/articleList.html');
感谢你能够认真阅读完这篇文章,希望小编分享的“ThinkPHP中Widget扩展如何调用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。