diff --git a/CHANGELOG.md b/CHANGELOG.md index effe3e9..3be809d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Yii1 Symfony Mailer extension ============================= +1.0.1 Under Development +----------------------- + +- Enh: Added passing email message instance via `$_message` context variable to the template (klimov-paul) +- Enh: Added passing template context variables to the layout template (klimov-paul) + + 1.0.0, May 10, 2024 ------------------- diff --git a/README.md b/README.md index 03d0146..9ea4586 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,29 @@ return [ ]; ``` +Inside the template file the following variables are always accessible: + +- `$this` - reference to `\yii1tech\mailer\View` instance. +- `$_message` - reference to email message instance, which is rendered. + +Template example: + +```php +subject('Email subject defined within the template'); + +$this->layout = 'particular-layout'; +?> +

Greetings

+

Context var "name" =

+``` + ### Writing unit tests diff --git a/src/Mailer.php b/src/Mailer.php index 74149d7..9b6c3f4 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -263,13 +263,21 @@ protected function render(RawMessage $message): RawMessage } if (($textTemplate = $message->getTextTemplate()) !== null) { - $text = $this->getView()->render($textTemplate, $message->getContext(), $message->getLocale()); + $text = $this->getView()->render( + $textTemplate, + array_merge($message->getContext(), ['_message' => $message]), + $message->getLocale() + ); $message->text($text); } if (($htmlTemplate = $message->getHtmlTemplate()) !== null) { - $html = $this->getView()->render($htmlTemplate, $message->getContext(), $message->getLocale()); + $html = $this->getView()->render( + $htmlTemplate, + array_merge($message->getContext(), ['_message' => $message]), + $message->getLocale() + ); $message->html($html); } diff --git a/src/View.php b/src/View.php index a74da97..b6d6fbc 100644 --- a/src/View.php +++ b/src/View.php @@ -184,7 +184,11 @@ public function render(string $view, ?array $data = null, ?string $locale = null $content = $this->renderPartial($view, $data, true); if (!empty($this->layout)) { - $content = $this->renderPartial($this->layout, ['content' => $content], true); + $content = $this->renderPartial( + $this->layout, + array_merge((array) $data, ['content' => $content]), + true + ); } } catch (\Throwable $e) { throw $e; diff --git a/tests/MailerTest.php b/tests/MailerTest.php index 32d8aae..3cb7376 100644 --- a/tests/MailerTest.php +++ b/tests/MailerTest.php @@ -122,6 +122,9 @@ public function testRender(): void $mailer = Yii::createComponent([ 'class' => Mailer::class, 'transport' => $transport, + 'view' => [ + 'layout' => 'layout', + ], ]); $email = (new TemplatedEmail()) @@ -140,5 +143,7 @@ public function testRender(): void $this->assertStringContainsString('Name = John Doe', $sentMessage->getTextBody()); $this->assertStringContainsString('Name = John Doe', $sentMessage->getHtmlBody()); + $this->assertStringContainsString('Subject = Test subject', $sentMessage->getHtmlBody()); + $this->assertStringContainsString('Layout-Subject = Test subject', $sentMessage->getHtmlBody()); } } \ No newline at end of file diff --git a/tests/views/mail/layout.php b/tests/views/mail/layout.php index 06c1fe3..96b9f43 100644 --- a/tests/views/mail/layout.php +++ b/tests/views/mail/layout.php @@ -1,9 +1,12 @@ Clip = getClips()->itemAt('test-clip') ?> +Layout-Subject = getSubject() : ''; ?> diff --git a/tests/views/mail/plain.php b/tests/views/mail/plain.php index c3f85a6..f895866 100644 --- a/tests/views/mail/plain.php +++ b/tests/views/mail/plain.php @@ -1,2 +1,9 @@ + Test plain mail template Name = +Subject = getSubject() : ''; ?> \ No newline at end of file diff --git a/tests/views/mail/switch.php b/tests/views/mail/switch.php index 1116c2e..428d23b 100644 --- a/tests/views/mail/switch.php +++ b/tests/views/mail/switch.php @@ -1,6 +1,7 @@ layout = 'layout';