How to create a Widget - Recent Comments widget


Here's a simple way to create a Recent comments showing widget to be used in your templates. The idea is just to highlighting your blog's new comments.

Step 1: - Creating a Widget Component


class Comments extends CPortlet
    public $title='Recent Comments';
    public $maxComments=10;
    public function getComments()
        return Comment::model()->findRecentComments($this->maxComments);
    protected function renderContent()

step 2 :- in components/views/comments.php

here comes the view file ('comments')

$comments = $this->getComments();
foreach($comments as $comment)
    echo "<li> {$comment->fieldName}</li>";

Step 3 :- in model model/Comment.php

public function findRecentComments($limit=null)
        return $this->findAll(array(
            'order'=>'t.create_time DESC',

Final step, Step 4 :- In your layout


$this->widget('Comments', array(

Happy Coding Yes It Is ........

Total 3 comments

#19120 report it
rajesh chaurasia at 2015/03/22 03:19am

very well explain..

#17578 report it
Rohit Suthar at 2014/07/04 07:55am

it's nice code..

#17449 report it
Daniel Galvan at 2014/06/13 03:32pm
Very Good

Simple but complete and clear example.

Leave a comment

Please to leave your comment.

Write new article