Confused - difference between widgets, portlets and components

I am a little confused as to the difference between these is?  Please can someone give me an example of where you would use each of these (and any other similar ways of incorporating repetitive code across multiple pages).  I specifically can't fathom out components from the manual.

A

Component is the general term referring to a highly reusable class. A widget or a portlet is a component.

Widget is a component that contains both logic and presentation.

Portlet is a special kind of widget that you usually see as a block on sidebar of a web page.

excellent, the same doubt was boring me yesterday!!!

great!!

Thanks Qiang, am I right in this understanding:

Presentation and logic eg. Most recent five posts = Widget

Logic only eg. Counting views (invisibly) for a page = Component

A Portlet is an example of a widget which has certain predefined properties - a title for example. 

And for just Presentation logic, would one simply include a view using $this->render('subViewName')?

Yes, if only presentation is needed and you want to reuse it, you can define it as a partial view and use renderPartial to render it at appropriate places.

Just one thing I would like to understand better.

This means that a widget has Presentation, Login AND Data?

Five recent posts needs to get the data from data base right?

You may want to do this:




class RecentPostWidget extends CWidget

{


    public $limit = 5;


    public function run() {

        $recentPosts = Post::model()->recently($this->limit)->published()->findAll();

        echo "<ul>";

        foreach ($recentPosts as $post) {

            echo "<li>" . $post->subject . "</li>";

        }

        echo "</ul>";

    }

}




<?php $this->widget("application.extensions.widgets.RecentPostWidget", array("limit" => 10)); ?>

Or you may want to separate the component and its view as follows. Investigate the code of an application of the yii-blogdemo-enhanced.

protected/components/RecentPosts.php:




<?php

class RecentPosts extends Portlet

{

  public $title='Recent Posts';


  public function getRecentPosts()

  {

    $criteria=array(

                    'condition'=>'Post.status='.self::STATUS_PUBLISHED,

                    'order'=>'Post.createTime DESC',

                    'limit'=>5,

                    );

    return Post::model()->findAll($criteria);

  }


  protected function renderContent()

  {

    $this->render('recentPosts');

  }

}



/protected/components/views/recentPosts.php:




<ul>

<?php foreach($this->getRecentPosts() as $post): ?>

<li>

<?php echo CHtml::link(CHtml::encode($post->title),array('post/show','id'=>$post->id)); ?>

&nbsp;on&nbsp;

<?php echo CHTml::link(date('M j', $post->createTime), array('post/PostedOnDate', 'time'=>$post->createTime)); ?>

</li>

<?php endforeach; ?>

</ul>



protected/views/layouts/main.php:


 

    <?php $this->widget('RecentPosts'); ?>



Hi Mocapapa,

I greatly like the idea of extending the Blog demo present in Yii. Are you looking for new members to your project? I think this can be a great learning experience for me.

Thanks.

Hello,

  i need your help. i want a ajax search function in my projects. i will use jquery. there will be a search box above with some other fields and a search button.after clicking this button search result will come below the page without page refreshing(not like cgrid or clist view).so for this which one i need create widget,component?

please Help!