Hi,
New to Yii and just wanted to post a solution I had for a question I saw a while back for incorporating layouts in the jonah’s mail extension. I wanted to template my emails in a similar fashion to how Yii uses layouts and views for web rendering. Just wanted to post if this helps someone out or if anybody has a better idea how to accomplish.
Thanks,
Steve
A couple modifications to the extension files:
[size="2"]YiiMailMessage.php[/size]
Add to class:
public $layout = 'main';
(override the setBody function)
public function setBody($body = '', $contentType = null, $charset = null) {
if ($this->view !== null) {
$bodyParams = (!is_array($body)) ? array('body'=>$body) : $body;
// if Yii::app()->controller doesn't exist create a dummy
// controller to render the view (needed in the console app)
$controller = (isset(Yii::app()->controller)) ? Yii::app()->controller : new CController('YiiMail');
// renderPartial won't work with CConsoleApplication, so use
// renderInternal - this requires that we use an actual path to the
// view rather than the usual alias
$viewPath = Yii::getPathOfAlias(Yii::app()->mail->viewPath.'.'.$this->view).'.php';
// If view file does not exist, then it's an empty string
if(file_exists($viewPath)){
$body = $controller->renderInternal(
Yii::getPathOfAlias(Yii::app()->mail->viewPath.'.'.$this->view).'.php',
array_merge($bodyParams, array('mail'=>$this)),
true
);
$layoutPath = Yii::getPathOfAlias(Yii::app()->mail->layoutPath.'.'.$this->layout).'.php';
if(file_exists($layoutPath)){
$body = $controller->renderInternal(
$layoutPath,
array_merge($bodyParams, array('content'=>$body)),
true
);
}
}
}
return $this->message->setBody($body, $contentType, $charset);
}
[size="2"]YiiMail.php[/size]
add to class:
public $layoutPath = 'application.views.layouts.mail';
[size="2"]main.php config:[/size]
Add the layoutPath to the mail config
'layoutPath' => 'application.views.layouts.mail',