Personalize your publication’s content material utilizing Mustache in Laravel

0


For Mustache and customized content material, it’s important to get began with Mustache, first (duuuh!)

Mustache makes use of Blade-like syntax (almooooost like…) to render the ultimate HTML content material. For instance, you possibly can show variables anyplace in your HTML:

[your content].
<hr>
Yow will discover us on our web site {{web site}}.

and you’ll set the webiste variable, to env('APP_URL') , for instance.

The syntax is easy, it might be one thing like that:

$mustache = new Mustache_Engine;
$renderedHTML = $mustache->render(
$request->html,
[
'web site' => env('APP_URL'),
]
);

Now, let’s create the unsubscribe button. Our Mustache variable might be unsubscribeUrl , and we’ll place it utilizing the HTML anchor tag. You are able to do no matter you want.

Set up the dependecy with Composer:

$ composer require mustache/mustache

First, we’ll get a greater have a look at our deal with() technique and render the HTML correctly. We’ll create an unsub button and we may also create a Mustache variable for person’s title, to personalize the expertise.

Additionally, we’ll render the topic too, so we will create one thing like “John, see our newest offers now!”

public operate deal with()
{
foreach(AppUser::all() as $person)
{
$mustache = new Mustache_Engine;
$renderedSubject = $mustache->render(
$request->topic,
[
'title' => $user->title,
]
);
$renderedHTML = $mustache->render(
$request->html,
[
'title' => $user->title,
'unsubscribeUrl' => url('/unsubscribe?user_id='.$user->id),
]
);
// Now, ship the mail with $renderedSubject and $renderedHTML.
}
}

Within the HTML, you’ll have one thing like:

{{title}}, for those who don't love our newsletters, you possibly can at all times <a href="{{unsubscribeUrl}}">unsubscribe from them</a>.
You might also like

Pin It on Pinterest

Share This

Share this post with your friends!