這篇文章主要介紹了ThinkPHP中Widget擴展的兩種寫法及調用方法,詳細分析了Widget擴展的寫法及相應的調用技巧,需要的朋友可以參考下
Widget擴展一般用於頁面元件的擴展,在頁面根據需要輸出不同的內容,下面介紹一下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');
PHP 隨機數C擴充隨機數字
#
以上是ThinkPHP中Widget擴充的兩種寫法及呼叫方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!