一:新建Widget文件(在index目录下) |
新建类Hot |
<?php |
Class HotWidget extends Widget{ |
Public function render ( $data ){ |
if (! $HotList =S( 'Hot_List' )){ |
$limit = $data [ 'limit' ]; |
$field = array ( 'id' , 'title' , 'hits' ); |
$where = array ( 'del' => 0); |
$HotList =M( 'blog' )->field( $field )->where( $where )->order( 'hits DESC' )->limit( $limit )->select(); |
S( 'Hot_List' , $HotList , 60, 'File' ); |
} |
$data [ 'hot' ] = $HotList ; |
return $this ->renderFile( '' , $data ); |
} |
} |
?> |
三:新建文件Hot.html(与类Hot同级) |
<dl> |
<dt></dt> |
< foreach name= 'hot' item= 'v' > |
<dd> |
<a href= "{:U('/'.$v['id'])}" >{ $v .title}</a> |
<span>({ $v .hits})</span> |
</dd> |
</ foreach > |
</dl> |
四:在模板中引用: |
{:W( 'Nav' )} |
{:W( 'Hot' , array ( 'limit' => 20))} //可以传参 |