Yii Widgets Vs CakePHP Elements

I am a bit confused about these. Widgets looks more powerful compared to elements however I am not sure if I understood them correctly. I have gone through the guide but I am still not sure. I am facing an issue and I feel probably widgets is the right way to go about it however I am not sure how it’ll work.

Here is the situation. There is a model called “Post” with associated “comments” and “favorites”. Listing of posts along with comments and favorite information need to be shown in many parts of the application. For example, application default page, user profile page (user’s post with comments and favorite information). I have everything working fine in post controller and it’s views.

I need to reuse the same view/code for displaying on user’s profile page but I don’t want to duplicate the views of post controller. Looks like if I create a widget for listing of posts with associated information then I can use it wherever needed. Is that correct? If so then how I need to go about it?

Can someone please throw some light on it.

Take a look at this widget as an example. In the end you probably want to come up with something like this:


<?php $this->widget('application.components.PostList', array(

  'maxPosts' => 10,

  'fullDetails' => false,

  ...

)); ?>

Within a widget you can use render() to render a widget specific view. Example:




$models = Post::model()->findAll();


$this->render('postList', array(

   'models' => $models,

));

Thanks. I’ll give it a try.