Different layouts for a CHttpException

Hello! I cant figure out how to specify a different layout for the CHttpException.

On my site im using several layouts, e.g. one for popups that i call generalPopup.php and other for normal site views, called main.php. I want that if an http error in a popup is shown, the error.php should use the layout generalPopup.php

Now the error is rendered with the main.php, that also contains header and footer. . How can i accomplish this? Using $this->layout = ‘generalPopup’ before the throw new CHttpException(404,‘Popup was not found’); doesnt work.

I implemented a quick and dirty function in SiteController, wich looks if the error message has popup string at the begining. But i dont want to use this solution on my production site. Has anyone a better idea?




	public function actionError()

	{

		if($error=Yii::app()->errorHandler->error) {

			error_log(substr($error['message'], 0, 5));

			if(strcasecmp(substr($error['message'], 0, 5),'popup') == 0) {

				$error['message'] = substr($error['message'], 5);

				$this->layout = 'generalPopup';

			}

			$this->render('error', array(

				'error' => $error

			));

		}

	}




You should have an error folder as part of your default Yii application where you can manipulate how the error template should look like.

The way I got around this problem was to create a new error action then set the apps errorHandler->errorAction to this new action.

So for my particular problem I wanted to have a different layout when an error occurred in a particular controller. So in the controller’s init() function I put this.


Yii::app()->errorHandler->errorAction='myController/error';

This could be done just before throwing the CHttpException, but in my case it was cleaner to put it in init(); The actionError in my second controller looked like this:


public function actionError()

{

   $this->layout = '//layouts/my_custom_layout';

   if($error=Yii::app()->errorHandler->error)

   {

 	if(Yii::app()->request->isAjaxRequest)

	    echo $error['message'];

	else

	    $this->render('../site/error', $error);

   }

}