Access Portlet Parameters Set in Controller

I am working on setting up my site using this format described by Qiang.

http://www.yiiframework.com/wiki/28/how-to-implement-multiple-page-layouts-in-an-application#hh1

Here he shows how to define controller properties to define the portlets to put into columns of a layout. I can get this to work for a portlet with no parameters.

What I can’t figure out is, if I want to pass parameters in the array shown in Qiang’s post as this…




<?php

$this->layout='column3';

$this->portlets['PostCategoryPortlet']=array();

$this->portlets['RecentCommentPortlet']=array('count'=>5);

?>

...render the post list...



… how would I access the ‘count’=>5 parameter within the portlet?

Thanks for any help.

OK, looked at this a bit more and I think I should have this in a view file not my controller… I’ll mess around with that for a while.

Alright, still stuck. Here’s what I’ve tried.

In a working view file:




$this->portlets1['RequiredFieldNote']=array('thing'=>'Hello World');



In a working portlet I want to access the array item "thing" in the view file.




// Tried getting "thing" this way...

<?php $x = Yii::app()->controller->portlets1['RequiredFieldNote']->thing; ?>

// And this way...

<?php $x = Yii::app()->controller->portlets1->RequiredFieldNote->thing; ?>

// And this way...

<?php $x = Yii::app()->controller->portlets1->requiredfieldnote->thing; ?>

// And this way...

<?php $x = Yii::app()->controller->requiredfieldnote->thing; ?>


  <p>My message: <?php echo $x; ?></p>



Maybe I’m going about this the wrong way, does anyone have any suggestions?

OK, I’ll preface this by saying I just started with Yii last week so I’m learning (and sometimes it just takes a conversation with yourself to figure things out). :rolleyes:

I’m just going to talk myself through how I followed what was going on here in case other newbies may benefit.

As I looked at the Yii guide examples more I realized that this array is being assigned (in the view file) to the "portlets" property of my controller, where the array key is the name of the widget class…

*** view file index.php




<?php 

    $this->portlets['MyNote']=array('note'=>'Hello World');  // assign value to note property.

    // $this->portlets['AnotherWidget']=array('blah'=>'yadda'); 

    // $this->portlets['MoreWidgets']=array();

?>


<h1>Home Page</h1>


<?php 

    $this->widget('zii.widgets.CListView', array(

            'dataProvider'=>$dataProvider,

            'itemView'=>'_view',

    )); 

?>



and then this code in the layout file creates the widgets by extracting the key as the widget class and the assigned array as the property values.

***layout file column2.php




<?php $this->beginContent('//layouts/main'); ?>

<div  class="container">

<!-- left-column -->    

    <div>

        <?php 

            foreach($this->portlets as $class=>$properties) 

            {

                    $this->widget($class,$properties); 

            }

        ?>

    </div>

<!-- content-column -->    

    <div>

        <?php echo $content; ?>

    </div>

</div>

<?php $this->endContent(); ?>



What I needed to do was add the properties to the widget class!!!

***widget class file MyNote.php (in components directory)




<?php

Yii::import('zii.widgets.CPortlet');

 

class MyNote extends CPortlet

{

    public $note;  // <----  THIS WAS THE KEY!!!

    

    public function init()

    {

        parent::init();

    }

 

    protected function renderContent()

    {

        $this->render('myNote');

    }

}

?>



and then my widget view can access its own property as such

***widget view file myNote.php (in components/views)




<?php 

    $x = $this->note;  // <--- NOW I CAN ACCESS IT HERE!!!

?>


    <p>My message: <?php echo $x; ?></p>



Lots of looking at this documentation helped me figure out the path.

http://www.yiiframework.com/wiki/28/how-to-implement-multiple-page-layouts-in-an-application#hh1

http://www.yiiframework.com/doc/guide/1.1/en/basics.view#widget

I’m sure the experienced OOP and Yii people out there will think this is obvious but I’m putting it all in here for the newbie that might find it helpful.

The above explanation really helped me! :)

One additional note; if you don’t want to set up an array of portlet data, you can pass it directly to the portlet.


$this->widget('MyNote', array('note'=>'Hello World'));

Just be sure you include the init() function in the widget class as described above.