I have a blog, in which there are different blog types, each blog type have many number of blog posts. I am passing the ID of blog type to get the detail of the blog post (a foreign key relation ship). For the sidebar I have created a widget, I would like to pass the blogtype ID to the widget so that I can show the latest posts of the corresponding blog type.
I have tried this in the view file
$this->widget('application.widget.blogs',array(blog_type_id=>$res_blog));
this in the widget
class Categories extends CWidget
{
public $blog_type;
public function run()
{
echo $blog_type;
}
}
But nothing seems to be working.
Can any one please look into the problem
Keep widgets on Components/Blogs.php
and create code like below .
class Blogs extends CWidget
{
public $blog_type_id;
public function run()
{
echo $this->blog_type_id;
// put other relevant code here.
}
}
And, you can call this with,
$this->widget('blogs',array(blog_type_id=>$res_blog));
If you put above class code inside as protected/widget/Blogs.php then you can call this widget like,
$this->widget('application.widget.blogs',array(blog_type_id=>$res_blog));
How to do this? My example...
Folder Structure:
Application (Your App Folder)
|
|
protected (Folder)
|
|
widget (Folder)
|
|
views (Folder)
| |
| blog.php (PHP View file)
|
Blog.php (PHP Class file)
Blog.php under protected/widget/
<?php
class Blog extends CWidget
{
public $dataProvider;
public function init()
{
// this method is called by CController::beginWidget()
}
public function run()
{
$dataSet=$this->dataProvider->getData();
$this->render('blog', array('dataSet'=>$dataSet));
}
}
?>
blog.php under protected/widget/views/
<!-- Your View as you want. Example: -->
<table>
<tr>
<th>Blog Name</th>
<th>Description</th>
</tr>
<?php foreach ($dataSet as $data): ?>
<tr>
<td><?= $data->name; ?></td>
<td><?= $data->description; ?></td>
</tr>
<?php endforeach; ?>
</table>
How to Use this widget
<?php
$dataProvider=new CActiveDataProvider('YourBlogModel'); //It has to come from controller
$this->widget('application.widget.Blog', array('dataProvider' => $dataProvider));
?>