David Moreno García
David Moreno García

Reputation: 4523

Generating HTML content in a Yii widget

I'm trying to make a Yii extension to fetch items from feed's urls stores in a table and then show them in a webpage. I'm a little confused with the framework and after search a lot I think that the best option is to use a widget. My problem with this is that all the items to show are stored in an array in the widget class. How can I create html content to show them? All I found are calls to render() method to show a custom view but I don't see a way to access my array from that view.

Upvotes: 1

Views: 1112

Answers (1)

Brett Gregson
Brett Gregson

Reputation: 5923

You need to pass the data (in this case, your array) to the view. So, assuming you are using a widget (extending CPortlet), something like so:

<?php
Yii::import('zii.widgets.CPortlet');
class MyWidget extends CPortlet
{
    public $my_array;

    protected function renderContent(){   
        $this->render("my_view",array(
            "my_array"  =>  $my_array
        ));
    }
}    

And then in your view (my_view, which will be in your components/views folder) you can access your array like so

$my_array[0]

When calling your widget (in another view), you can pass it your array like so:

$this->widget("MyWidget",array("my_array" => $my_array));

Hope that answers your question

Upvotes: 1

Related Questions