Two ways of writing and calling widget extensions in thinkphp

Source: Internet
Author: User
This article mainly introduces the two methods of writing and invoking the widget extension in thinkphp, analyzes the method of the widget extension and the corresponding calling skill, and the friend who needs to refer to the following

Widget extensions are generally used to extend the page components, and to output different content on the page as needed, here's a look at the two ways in which widgets are written and called in thinkphp

Writing one:

ArticlWidget.class.php file:

Class Articlewidget extends Widget {  /**   * *   @param array $data   * @return Type   * Call method: {: W (' Articlelist ', Array (' CID ' =>25, ' limit ' =>5)}   * CID for classification Id,limit for 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);  }}

Template file articlelist.html in 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>

Two:

Class Articlewidget extends Action {  /**   *   * @param array $data   * @return Type   * Call method: {: R (' article/ Articlelist ', Array (' CID ' =>25, ' limit ' =>5), ' Widget ')}   * CID for classification Id,limit for 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, content same as one, but placed in tpl/style name/widget/directory

If the template file is placed under the article folder in the directory where the ArticleWiget.class.php file is located, the following is the wording:

$this->display (DirName (__file__). '/article/articlelist.html ');

Related recommendations:

PHP pre-defined variables 9 large Super Global array usage detailed _php basics

PHP Random number C Extended random number

Understanding PHP Dependency Injection and control reversal _php tips

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.