search
Homephp教程PHP开发ThinkPHP普通分组下Widget的使用

在ThinkPHP中Widget工具的使用很简单,调用起来也非常方便。我们就以调用热点文章为例说一下Widget的使用。因为wblog博客程序是普通分组的,所以下面我就在ThinkPHP普通分组下使用Widget工具。


下载wblog3.1.3版本


因为wblog一安装,什么数据库啊,数据表啊都有了,不用再麻烦去创建。


创建Widget相关目录和文件

按照下面的目录结构去创建相关的Widget目录和文件。

├─w3note普通分组项目目录

│  ├─Common

│  ├─Conf

│  ├─Lang

├─├─Lib 

│  │   ├─Action 

│  │   ├─Behavior 

│  │   ├─Model 

│  │   ├─Widget 

│  ├─Tpl    ├─HotWidget.class.php

│  │         ├─Hot

│  │             ├─Hot.html


HotWidget.class.php文件的代码

<?php
// 首页
class HotWidget extends Widget {
	public function render($data) {
		$data[&#39;hot&#39;]=M(&#39;News&#39;)->field(&#39;id,title,hits&#39;)->order(&#39;hits desc&#39;)->limit

(8)->select();
		return $this->renderFile(&#39;&#39;,$data);
	}
	
}
?>

Hot.html文件的代码

<volist name="hot" id="h">
    <li>  <a title="{$h[&#39;title&#39;]}" href="{:U(&#39;/read/&#39;.$h[&#39;id&#39;])}">{$h[&#39;title&#39;]

|msubstr=0,30,&#39;utf-8&#39;}</a></li>
 </volist>

调用Widget工具


Widget工具的调用比较灵活,可以放在首页,列表页,内容页。下面我就把它放在内容页,打开W3note\Tpl\Read\read.html文件,使用{:W('Hot')}来调用我们上面创建的Widget工具。代码如下

<div class="l_tit">热点文章</div>
      <div class="rand">
        <ul>
         {:W(&#39;Hot&#39;)}
        </ul>
 </div>

最终效果图

ThinkPHP普通分组下Widget的使用


小结:一般情况下,我们调用热点文章,需要在控制器创建一条语句,然后在模板上写循环语句。但如果在多个页面上调用,这种方法显得比较繁琐,而使用Widget工具恰好能轻松的解决这一问题。




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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools