Reusable View Element

I’m somewhat new to Yii coming from using CakePHP quite a bit.

I’m wondering if there is some sort of reusable view element

like Cake’s $this->element(‘element_name’, array(‘var1’ => ‘val’, ‘var2’ => ‘val’));

I know Yii has widgets but you have to create a whole class file

for each widget when I really just want a reusable view that I can

drop on different pages.

It’s basically a table of data that doesn’t need any kind of formatting,

just a loop that spits out each row in the table.

Does Yii have anything like this?


CController.renderPartial() is what you are looking for if you don’t want to create a widget.

It is a good practice to name the view starting with an underscore e.g. _myView

But if you are using the view a lot in other different views I would create a Widget for it.

Great, thanks for the heads up.

How would I reference the path to the view file

I want to render the partial view of?

Would I do something like this in my primary view file:

$this->renderPartial(’[WHAT DO I PUT FOR THE PATH]’, array(‘var1’ => ‘val’, ‘var2’ => ‘val’));

Let me know if this is right and how I would access

different partial view files in various directories

of my theme.

Thanks for the help.

I assume that the partial view _view1.php is located in protected/your_theme_name/views/partial directory

$this->renderPartial('/partial/_view1', array('var1' => 'val', 'var2' => 'val'));

For further informations look at the description/code of the CController.getViewFile() method.

Great, thanks this is exactly what I was looking for.