render shared partial without using widget or porlet

Consider the following senario :

You have a controller inside a module - defaultController, in which there is this action - actionIndex, this action renders the view which resides in views/default/index.php, thus the folder structure looks like :


  - module1

     - controllers

       - defaultController.php

     - views

       - default

         - index.php

now in the index.php view, I want to include a partial from another module - module2, let’s call the view “anotherView.php”, how do I include it?

I’ve tried to put the following code in index.php

<?php $this->renderPartial('anotherView', array('params'=>...)) ?>

But that seemed not working since Yii seems to be looking for anotherView in module1/views/default/anotherView.php

One solution seems to be using the widget, that is, to be specific, to move module2/views/.../anotherView.php into application/components/views/anotherView.php and then declare a widget in application/components/AnotherView.php and then use it in index.php as :

<?php $this->widget('AnotherView', array('params'=>...)); ?>

My question is how do I directly fetch any arbitrary view from any views folder and render it in my current view without using a widget or portlet?

I don’t want to sound too discouraging, but in my opinion, this problem evolves from bad design. It should never be necessary to render a view from another module as a module should be self contained. This is IMHO, of course.

If you can solve it with a widget, that’s much cleaner.

@Mike: Thanks for pointing it out. Actually I had the similar feeling. I came from the symfony world, there the views can render any other partials from arbitrary locations. In symfony "components" are defined as self-contain parts which maps to widgets in Yii, still I think it provides more flexibility if Yii can support arbitrary view rendering, especially during the prototyping phase of a project, you may not want to convert everything into widgets.

Just for the record: I didn’t say, it’s not possible, i just never thought about wether and how to do it. So if you still need it, do some research on the forum here, i think this question already came up several times.

I am of the same opinion of Max, that render view from another module (and even from another controller) is a bad idea.

Aniway you can do like that:


That’s true.