2013-12-23 43 views
2

我有一個博客,其中有不同的博客類型,每個博客類型都有許多博客帖子。我通過博客類型的ID來獲取博客文章(外鍵關係船)的詳細信息。對於側邊欄我創建了一個小部件,我想將博客類型ID傳遞給小部件,以便我可以顯示相應博客類型的最新帖子。發送查看文件中的值到小部件yii

我在小部件

class Categories extends CWidget 
{ 
public $blog_type; 
public function run() 
{ 
    echo $blog_type; 
} 
} 

但沒有什麼似乎是工作在視圖文件

$this->widget('application.widget.blogs',array(blog_type_id=>$res_blog)); 

這種嘗試這樣做。

任何一個可以請查問題

回答

1

繼續Components/Blogs.php widgets和下面一樣創建代碼。

class Blogs extends CWidget 
{ 
    public $blog_type_id; 
    public function run() 
    { 
    echo $this->blog_type_id; 
    // put other relevant code here. 
    } 
} 

而且,你可以把這個,

$this->widget('blogs',array(blog_type_id=>$res_blog)); 

如果你把上面的類代碼中爲保護/空間/ Blogs.php,那麼你可以調用這個小部件一樣,

$this->widget('application.widget.blogs',array(blog_type_id=>$res_blog)); 
2

如何做到這一點?我的例子...

文件夾結構:

 Application (Your App Folder) 
      | 
      | 
      protected (Folder) 
       | 
       | 
       widget (Folder) 
        | 
        | 
        views (Folder) 
        | | 
        | blog.php (PHP View file) 
        | 
        Blog.php (PHP Class file) 

blog.php的保護/小工具下/下保護/小工具/視圖

 <?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的/

 <!-- 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 t o使用此小工具

 <?php 
     $dataProvider=new CActiveDataProvider('YourBlogModel'); //It has to come from controller 
     $this->widget('application.widget.Blog', array('dataProvider' => $dataProvider)); 
     ?>