diff --git a/framework/yii/mail/BaseMailer.php b/framework/yii/mail/BaseMailer.php index e5a1dde..4d61fd6 100644 --- a/framework/yii/mail/BaseMailer.php +++ b/framework/yii/mail/BaseMailer.php @@ -20,7 +20,6 @@ use yii\base\ViewContextInterface; * @see BaseMessage * * @property \yii\base\View|array $view view instance or its array configuration. - * @property \yii\mail\ViewResolver|array $viewResolver view resolver instance or its array configuration. * * @author Paul Klimov * @since 2.0 @@ -36,6 +35,14 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont */ public $viewPath = '@app/mailviews'; /** + * @var string HTML layout view name. + */ + public $htmlLayout = 'layouts/html'; + /** + * @var string text layout view name. + */ + public $textLayout = 'layouts/text'; + /** * @var array configuration, which should be applied by default to any new created * email message instance. * For example: @@ -128,11 +135,17 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont * Renders a view. * @param string $view the view name or the path alias of the view file. * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. - * @return string string the rendering result + * @param string|boolean $layout layout view name, if false given no layout will be applied. + * @return string the rendering result. */ - public function render($view, $params = []) + public function render($view, $params = [], $layout = false) { - return $this->getView()->render($view, $params, $this); + $output = $this->getView()->render($view, $params, $this); + if ($layout !== false) { + return $this->getView()->render($layout, ['content' => $output], $this); + } else { + return $output; + } } /** diff --git a/framework/yii/mail/BaseMessage.php b/framework/yii/mail/BaseMessage.php index a641521..4952721 100644 --- a/framework/yii/mail/BaseMessage.php +++ b/framework/yii/mail/BaseMessage.php @@ -36,7 +36,7 @@ use Yii; abstract class BaseMessage extends Object implements MessageInterface { /** - * @return \yii\mail\BaseMailer + * @return \yii\mail\BaseMailer mailer component instance. */ public function getMailer() { @@ -54,8 +54,18 @@ abstract class BaseMessage extends Object implements MessageInterface /** * @inheritdoc */ - public function render($view, $params = []) + public function renderHtml($view, $params = []) { - return $this->getMailer()->render($view, $params); + $this->setHtml($this->render($view, $params, $this->getMailer()->htmlLayout)); + return $this; + } + + /** + * @inheritdoc + */ + public function renderText($view, $params = []) + { + $this->setText($this->render($view, $params, $this->getMailer()->textLayout)); + return $this; } } \ No newline at end of file diff --git a/framework/yii/mail/MessageInterface.php b/framework/yii/mail/MessageInterface.php index 171c4f3..301a8d1 100644 --- a/framework/yii/mail/MessageInterface.php +++ b/framework/yii/mail/MessageInterface.php @@ -122,15 +122,26 @@ interface MessageInterface public function send(); /** - * Renders a view. + * Fills up HTML body rendering a view. * The view to be rendered can be specified in one of the following formats: - * - path alias (e.g. "@app/emails/contact/body"); - * - relative path (e.g. "contact"): the actual view file will be resolved by [[resolveView]]. + * - path alias (e.g. "@app/mails/contact/body"); + * - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]]. * @param string $view the view name or the path alias of the view file. * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. - * @return string string the rendering result + * @return static self reference. */ - public function render($view, $params = []); + public function renderHtml($view, $params = []); + + /** + * Fills up plain text body rendering a view. + * The view to be rendered can be specified in one of the following formats: + * - path alias (e.g. "@app/mails/contact/body"); + * - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]]. + * @param string $view the view name or the path alias of the view file. + * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. + * @return static self reference. + */ + public function renderText($view, $params = []); /** * String output. diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index 1781657..f2d3762 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -134,9 +134,9 @@ class BaseMailerTest extends TestCase $mailer->viewPath = $filePath; $viewName = 'test_view'; - $fileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; - $fileContent = ''; - file_put_contents($fileName, $fileContent); + $viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; + $viewFileContent = ''; + file_put_contents($viewFileName, $viewFileContent); $params = [ 'testParam' => 'test output' @@ -144,6 +144,30 @@ class BaseMailerTest extends TestCase $renderResult = $mailer->render($viewName, $params); $this->assertEquals($params['testParam'], $renderResult); } + + /** + * @depends testRender + */ + public function testRenderLayout() + { + $mailer = new Mailer(); + + $filePath = $this->getTestFilePath(); + $mailer->viewPath = $filePath; + + $viewName = 'test_view'; + $viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; + $viewFileContent = 'view file content'; + file_put_contents($viewFileName, $viewFileContent); + + $layoutName = 'test_layout'; + $layoutFileName = $filePath . DIRECTORY_SEPARATOR . $layoutName . '.php'; + $layoutFileContent = 'Begin Layout End Layout'; + file_put_contents($layoutFileName, $layoutFileContent); + + $renderResult = $mailer->render($viewName, [], $layoutName); + $this->assertEquals('Begin Layout ' . $viewFileContent . ' End Layout', $renderResult); + } } /**