Skip to content

Commit

Permalink
enhance template context var processing
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed May 20, 2024
1 parent 769b6af commit ef2ae82
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-------------------

Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
/**
* @var $this \yii1tech\mailer\View
* @var $_message \yii1tech\mailer\TemplatedEmail
* @var $name string
*/

$_message->subject('Email subject defined within the template');

$this->layout = 'particular-layout';
?>
<h1>Greetings</h1>
<p>Context var "name" = <?php echo CHtml::encode($name) ?></p>
```


### Writing unit tests <span id="writing-unit-tests"></span>

Expand Down
12 changes: 10 additions & 2 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions tests/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public function testRender(): void
$mailer = Yii::createComponent([
'class' => Mailer::class,
'transport' => $transport,
'view' => [
'layout' => 'layout',
],
]);

$email = (new TemplatedEmail())
Expand All @@ -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());
}
}
3 changes: 3 additions & 0 deletions tests/views/mail/layout.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
/**
* @var $this \yii1tech\mailer\View
* @var $_message \yii1tech\mailer\TemplatedEmail|null
* @var $content string
*/
?>
<!--Header-->
<?php echo $content; ?>
Clip = <?php echo $this->getClips()->itemAt('test-clip') ?>
Layout-Subject = <?php echo isset($_message) ? $_message->getSubject() : ''; ?>
<!--Footer-->
7 changes: 7 additions & 0 deletions tests/views/mail/plain.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
<?php
/**
* @var $this \yii1tech\mailer\View
* @var $_message \yii1tech\mailer\TemplatedEmail|null
*/
?>
Test plain mail template
Name = <?php echo $name; ?>
Subject = <?php echo isset($_message) ? $_message->getSubject() : ''; ?>
1 change: 1 addition & 0 deletions tests/views/mail/switch.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @var $this \yii1tech\mailer\View
* @var $name string
*/

$this->layout = 'layout';
Expand Down

0 comments on commit ef2ae82

Please sign in to comment.