Arabic | English | Spanish | Indonesian | Japanese | Polish | Portuguese | Russian | Ukrainian | Simplified Chinese
创建最新评论 Portlet
此节中,我们创建最后一个 portlet ,它将显示最新发布的评论列表。
创建 RecentComments
类
我们将 RecentComments
类创建在文件 /wwwroot/blog/protected/components/RecentComments.php
中。此文件内容如下:
Yii::import('zii.widgets.CPortlet'); class RecentComments extends CPortlet { public $title='Recent Comments'; public $maxComments=10; public function getRecentComments() { return Comment::model()->findRecentComments($this->maxComments); } protected function renderContent() { $this->render('recentComments'); } }
如上所示,我们调用了 Comment
类中定义的 findRecentComments
方法。此方法代码如下:
class Comment extends CActiveRecord { ...... public function findRecentComments($limit=10) { return $this->with('post')->findAll(array( 'condition'=>'t.status='.self::STATUS_APPROVED, 'order'=>'t.create_time DESC', 'limit'=>$limit, )); } }
创建 recentComments
视图
recentComments
视图存储在文件 /wwwroot/blog/protected/components/views/recentComments.php
中。它只是简单的显示由 RecentComments::getRecentComments()
方法返回的每一条评论。
使用 RecentComments
Portlet
我们修改布局文件 /wwwroot/blog/protected/views/layouts/column2.php
插入最后这个 portlet,
...... <div id="sidebar"> <?php if(!Yii::app()->user->isGuest) $this->widget('UserMenu'); <?php $this->widget('TagCloud', array( 'maxTags'=>Yii::app()->params['tagCloudCount'], )); <?php $this->widget('RecentComments', array( 'maxComments'=>Yii::app()->params['recentCommentCount'], )); </div> ......
$Id: portlet.comments.txt 1773 2010-02-01 18:39:49Z qiang.xue $