Two Methods of writing and calling the Widget extension in ThinkPHP, thinkphpwidget

Source: Internet
Author: User

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');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.