Commit 368c2447 by Paul Klimov

MessageInterface::render() replaced by MessageInterface::renderHtml() and…

MessageInterface::render() replaced by MessageInterface::renderHtml() and MessageInterface::renderText()
parent 4c27d887
...@@ -20,7 +20,6 @@ use yii\base\ViewContextInterface; ...@@ -20,7 +20,6 @@ use yii\base\ViewContextInterface;
* @see BaseMessage * @see BaseMessage
* *
* @property \yii\base\View|array $view view instance or its array configuration. * @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 <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0 * @since 2.0
...@@ -36,6 +35,14 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -36,6 +35,14 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
*/ */
public $viewPath = '@app/mailviews'; 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 * @var array configuration, which should be applied by default to any new created
* email message instance. * email message instance.
* For example: * For example:
...@@ -128,11 +135,17 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -128,11 +135,17 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* Renders a view. * Renders a view.
* @param string $view the view name or the path alias of the view file. * @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. * @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;
}
} }
/** /**
......
...@@ -36,7 +36,7 @@ use Yii; ...@@ -36,7 +36,7 @@ use Yii;
abstract class BaseMessage extends Object implements MessageInterface abstract class BaseMessage extends Object implements MessageInterface
{ {
/** /**
* @return \yii\mail\BaseMailer * @return \yii\mail\BaseMailer mailer component instance.
*/ */
public function getMailer() public function getMailer()
{ {
...@@ -54,8 +54,18 @@ abstract class BaseMessage extends Object implements MessageInterface ...@@ -54,8 +54,18 @@ abstract class BaseMessage extends Object implements MessageInterface
/** /**
* @inheritdoc * @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
...@@ -122,15 +122,26 @@ interface MessageInterface ...@@ -122,15 +122,26 @@ interface MessageInterface
public function send(); 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: * The view to be rendered can be specified in one of the following formats:
* - path alias (e.g. "@app/emails/contact/body"); * - path alias (e.g. "@app/mails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[resolveView]]. * - 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 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. * @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. * String output.
......
...@@ -134,9 +134,9 @@ class BaseMailerTest extends TestCase ...@@ -134,9 +134,9 @@ class BaseMailerTest extends TestCase
$mailer->viewPath = $filePath; $mailer->viewPath = $filePath;
$viewName = 'test_view'; $viewName = 'test_view';
$fileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php'; $viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php';
$fileContent = '<?php echo $testParam; ?>'; $viewFileContent = '<?php echo $testParam; ?>';
file_put_contents($fileName, $fileContent); file_put_contents($viewFileName, $viewFileContent);
$params = [ $params = [
'testParam' => 'test output' 'testParam' => 'test output'
...@@ -144,6 +144,30 @@ class BaseMailerTest extends TestCase ...@@ -144,6 +144,30 @@ class BaseMailerTest extends TestCase
$renderResult = $mailer->render($viewName, $params); $renderResult = $mailer->render($viewName, $params);
$this->assertEquals($params['testParam'], $renderResult); $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 <?php echo $content; ?> End Layout';
file_put_contents($layoutFileName, $layoutFileContent);
$renderResult = $mailer->render($viewName, [], $layoutName);
$this->assertEquals('Begin Layout ' . $viewFileContent . ' End Layout', $renderResult);
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment