0 follower

Final Class Yiisoft\Mailer\View\MessageBodyRenderer

InheritanceYiisoft\Mailer\View\MessageBodyRenderer

View renderer used to compose message body.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Mailer\View\MessageBodyRenderer
addBodyToMessage() Adds the rendered body to the message and returns it. Yiisoft\Mailer\View\MessageBodyRenderer
renderHtml() Renders the HTML view specified with optional parameters and layout. Yiisoft\Mailer\View\MessageBodyRenderer
renderText() Renders the text view specified with optional parameters and layout. Yiisoft\Mailer\View\MessageBodyRenderer
withLocale() Returns a new instance with specified locale code. Yiisoft\Mailer\View\MessageBodyRenderer
withTemplate() Returns a new instance with the specified message body template. Yiisoft\Mailer\View\MessageBodyRenderer
withView() Returns a new instance with the specified view. Yiisoft\Mailer\View\MessageBodyRenderer

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\View\View $view, Yiisoft\Mailer\View\MessageBodyTemplate $template )
$view \Yiisoft\View\View

The view instance.

$template Yiisoft\Mailer\View\MessageBodyTemplate

The message body template instance.

                public function __construct(
    private View $view,
    private MessageBodyTemplate $template,
) {
}

            
addBodyToMessage() public method

Adds the rendered body to the message and returns it.

public \Yiisoft\Mailer\MessageInterface addBodyToMessage ( \Yiisoft\Mailer\MessageInterface $message, string $htmlView, array $viewParameters = [], array $layoutParameters = [], string|null $textView null )
$message \Yiisoft\Mailer\MessageInterface

The message to which the body will be added.

$htmlView string

The view name to be used for rendering the message HTML body.

$viewParameters array

The parameters (name-value pairs) that will be extracted and available in the view file.

$layoutParameters array

The parameters (name-value pairs) that will be extracted and available in the layout file.

$textView string|null

The view name to be used for rendering the message text body.

return \Yiisoft\Mailer\MessageInterface

The message with the added body.

throws \Yiisoft\View\Exception\ViewNotFoundException

If the view file does not exist.

                public function addBodyToMessage(
    MessageInterface $message,
    string $htmlView,
    array $viewParameters = [],
    array $layoutParameters = [],
    ?string $textView = null,
): MessageInterface {
    $message = $message->withHtmlBody(
        $this->renderHtml($htmlView, $viewParameters, $layoutParameters)
    );
    if ($textView !== null) {
        $message = $message->withTextBody(
            $this->renderText($textView, $viewParameters, $layoutParameters)
        );
    }
    return $message;
}

            
renderHtml() public method

Renders the HTML view specified with optional parameters and layout.

See also \Yiisoft\View\View::render().

public string renderHtml ( string $view, array $viewParameters = [], array $layoutParameters = [] )
$view string

The view name of the view file.

$viewParameters array

The parameters (name-value pairs) that will be extracted and available in the view file.

$layoutParameters array

The parameters (name-value pairs) that will be extracted and available in the layout file.

return string

The rendering HTML result.

throws \Yiisoft\View\Exception\ViewNotFoundException

If the view file does not exist.

                public function renderHtml(string $view, array $viewParameters = [], array $layoutParameters = []): string
{
    $content = $this->view
        ->withContextPath($this->template->viewPath)
        ->render($view, $viewParameters);
    if ($this->template->htmlLayout === null) {
        return $content;
    }
    $layoutParameters['content'] = $content;
    return $this->view
        ->withContextPath($this->template->viewPath)
        ->render($this->template->htmlLayout, $layoutParameters);
}

            
renderText() public method

Renders the text view specified with optional parameters and layout.

See also \Yiisoft\View\View::render().

public string renderText ( string $view, array $viewParameters = [], array $layoutParameters = [] )
$view string

The view name of the view file.

$viewParameters array

The parameters (name-value pairs) that will be extracted and available in the view file.

$layoutParameters array

The parameters (name-value pairs) that will be extracted and available in the layout file.

return string

The rendering text result.

throws \Yiisoft\View\Exception\ViewNotFoundException

If the view file does not exist.

                public function renderText(string $view, array $viewParameters = [], array $layoutParameters = []): string
{
    $content = $this->view
        ->withContextPath($this->template->viewPath)
        ->render($view, $viewParameters);
    if ($this->template->textLayout === null) {
        return $content;
    }
    $layoutParameters['content'] = $content;
    return $this->view
        ->withContextPath($this->template->viewPath)
        ->render($this->template->textLayout, $layoutParameters);
}

            
withLocale() public method

Returns a new instance with specified locale code.

public self withLocale ( string $locale )
$locale string

The locale code.

                public function withLocale(string $locale): self
{
    $new = clone $this;
    $new->view = $this->view->withLocale($locale);
    return $new;
}

            
withTemplate() public method

Returns a new instance with the specified message body template.

public self withTemplate ( Yiisoft\Mailer\View\MessageBodyTemplate $template )
$template Yiisoft\Mailer\View\MessageBodyTemplate

The message body template.

return self

The new instance.

                public function withTemplate(MessageBodyTemplate $template): self
{
    $new = clone $this;
    $new->template = $template;
    return $new;
}

            
withView() public method

Returns a new instance with the specified view.

public self withView ( \Yiisoft\View\View $view )
$view \Yiisoft\View\View

The view instance.

return self

The new instance.

                public function withView(View $view): self
{
    $new = clone $this;
    $new->view = $view;
    return $new;
}