Sharing views between controllers

How to share one view between two or more controllers? In simple way, if I call render (or renderPartial) function it searches for given template in directory of current controller’s views. How to render other controller’s view? Or maybe I could place common views in some other directory?

I know I can achieve this making that common view to be a widget and then run it in many views of many controllers, but I think it’s a little abuse, when the common view has no logic, only static content.

It’s as simple as:


You can call renderPartial()/render() on any view from anywhere as long as you have reference to a controller.

Read here how does Yii search for a view file.

This doesn’t work for me. The error message is "SiteController cannot find the requested view “apartment/_searchForm”.

However, I’ve discovered that when I do it like that:


so with "/" at the begining, it works fine. Is that correct, or I have sth wrong in my views structure?

Ok, I think I understand :) The path ‘apartment/_searchForm’ searches for view in apartment subdirectory of the current controller’s view directory, while ‘/apartment/_searchForm’ searches for view starting from base view dir.

Ahh, I apologize for leaving out the initial slash :-X

Maybe is better to create a widget that render this view.

I always use this approach because I like breadcrumbs, so i often have view that are really similar except breadcrumbs and menu. In such cases is really confortable to create a widget for the shared part and write 2 views.

The only diffrence is that instead of call renderPartial I call new widget.