>  기사  >  백엔드 개발  >  ThinkPHP에서 위젯 확장을 작성하고 호출하는 두 가지 방법

ThinkPHP에서 위젯 확장을 작성하고 호출하는 두 가지 방법

墨辰丷
墨辰丷원래의
2018-05-23 09:37:391448검색

이 글에서는 ThinkPHP에서 위젯 확장 기능을 작성하고 호출하는 두 가지 방법을 주로 소개하고, 위젯 확장 기능이 필요한 친구들이 참고할 수 있는 작성 방법과 그에 따른 호출 기법을 자세히 분석합니다. 페이지 구성요소 필요에 따라 페이지에 다른 콘텐츠를 출력합니다. ThinkPHP에서 위젯을 작성하고 호출하는 두 가지 방법이 있습니다.


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>

작성 방법 2:

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;);
  }
}
템플릿 파일 ArticleList.html, 내용은 글쓰기와 동일합니다. 방법 1, Tpl/style name/에 위치 Widget/ 디렉터리 아래

템플릿 파일을 ArticleWiget.class.php 파일이 있는 디렉터리의 Article 폴더에 넣으면 다음과 같이 작성됩니다.

$this->display(dirname(__FILE__) . &#39;/Article/articleList.html&#39;);

관련 권장 사항:

PHP

사전 정의된 변수 9개의 슈퍼 전역 arrays_php 기본 사용법에 대한 자세한 설명

PHP 난수 C 확장 난수

PHP 이해하기 control_php 스킬의 의존성 주입과 반전


위 내용은 ThinkPHP에서 위젯 확장을 작성하고 호출하는 두 가지 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.