Also available in these languages:
English日本語polskiРусский简体中文

Creating Recent Comments Portlet

In this section, we create the last portlet that displays a list of comments recently published.

Creating RecentComments Class

We create the RecentComments class in the file /wwwroot/blog/protected/components/RecentComments.php. The file has the following content:

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');
    }
}

In the above we invoke the findRecentComments method which is defined in the Comment class as follows,

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,
        ));
    }
}

Creating recentComments View

The recentComments view is saved in the file /wwwroot/blog/protected/components/views/recentComments.php. It simply displays every comment returned by the RecentComments::getRecentComments() method.

Using RecentComments Portlet

We modify the layout file /wwwroot/blog/protected/views/layouts/column2.php to embed this last 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 $
If you find any typos or errors in the tutorial, please create a Yii ticket to report it. If it is a translation error, please create a Yiidoc ticket, instead. Thank you.

Total 1 comment:

#1489
This example could be more usefull if only it tells how to interact portlets and actions...
by revo110 at 5:07am on May 15, 2010.

Hi,

This example could be more usefull if only it shows how to interact with actions from portlets.

Maybe, add a new comment action ?

Thanks a bunch, and I do hope that there will be an example for this.

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.