Home >Backend Development >PHP Tutorial >Two ways to write and call Widget extensions in ThinkPHP

Two ways to write and call Widget extensions in ThinkPHP

墨辰丷
墨辰丷Original
2018-05-23 09:37:391525browse

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(&#39;Article/articleList&#39;,array(&#39;cid&#39;=>25,&#39;limit&#39;=>5),&#39;Widget&#39;)}
   * cid为分类id,limit为调用数量
   */
  public function articleList($cid, $limit) {
    $Article = M(&#39;Article&#39;);
    $articleMap["cid"] = $cid;
    $data = $Article->where($articleMap)->order(&#39;id desc&#39;)->limit($limit)->select();
    foreach ($data as $key => $value) {
      if ($value["thumbnail"] == "") {
        $data[$key]["thumbnail"] = &#39;/Public/Img/Common/noThumbnail.jpg&#39;;
      }
    }
    $this->assign(&#39;articleList&#39;, $data);
    $this->display(&#39;Widget:articleList&#39;);
  }
}

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__) . &#39;/Article/articleList.html&#39;);

Related recommendations:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn