Browse Source

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

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
368c244768
  1. 21
      framework/yii/mail/BaseMailer.php
  2. 16
      framework/yii/mail/BaseMessage.php
  3. 21
      framework/yii/mail/MessageInterface.php
  4. 30
      tests/unit/framework/mail/BaseMailerTest.php

21
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 <klimov.paul@gmail.com>
* @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;
}
}
/**

16
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;
}
}

21
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.

30
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 = '<?php echo $testParam; ?>';
file_put_contents($fileName, $fileContent);
$viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php';
$viewFileContent = '<?php echo $testParam; ?>';
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 <?php echo $content; ?> End Layout';
file_put_contents($layoutFileName, $layoutFileContent);
$renderResult = $mailer->render($viewName, [], $layoutName);
$this->assertEquals('Begin Layout ' . $viewFileContent . ' End Layout', $renderResult);
}
}
/**

Loading…
Cancel
Save