Skip to content

Instantly share code, notes, and snippets.

@inxilpro
Last active January 22, 2024 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inxilpro/0e150aaa7c740016bad0fe8c94fa6e17 to your computer and use it in GitHub Desktop.
Save inxilpro/0e150aaa7c740016bad0fe8c94fa6e17 to your computer and use it in GitHub Desktop.
View hooks demo
<?php
class Promotions
{
public static function for(User $user)
{
return new static($user);
}
public function __construct(protected User $user)
{
}
public function registerHooks()
{
if ($this->inCyberMondayWindow()) {
View::hook('mail.receipt', 'promos', function() {
return new HtmlString(Blade::render(<<<'BLADE'
<x-mail-banner>
Don't miss out on our <a href="#">Cyber Monday deals!</a>
</x-mail-banner>
BLADE
));
});
}
}
protected function inCyberMondayWindow()
{
$start = Date::parse('fourth monday of november')->toImmutable();
$end = $start->addWeek();
return now()->between($start, $end);
}
}
<x-email>
<x-hook name="promos" />
<p>Thank you for your purchase!</p>
...
</x-email>
<?php
class ReceiptMail extends Mailable
{
public function __construct(
public User $customer,
public Collection $items
) {
}
public function build()
{
Promotions::for($this->customer)->registerHooks();
}
public function content(): Content
{
return new Content(view: 'mail.receipt');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment