Gen. partnerAlgotech

How to render a Latte template to a string

The Latte templating system is suitable for rendering almost all types of templates on the web. For rendering frontend templates, for example, React or Vue.js has been the best choice for the last few years, but for rendering email templates on the backend, Latte still wins.

|
December 18, 2022

The Latte templating system is suitable for rendering almost all types of templates on the web. For rendering frontend templates, for example, React or Vue.js has been the best choice for the last few years, but for rendering email templates on the backend, Latte still wins.

So how do we ensure we render a specific HTML template to a string that we can send via email?

Easy:

$latte = new Engine();
$latte->setLoader(new StringLoader());
$template = '<p>My name is: {$firstName}:{$lastName}!</p>';
$html = $latte->renderToString($template, [
'firstName' => 'Jan',
'lastName' => 'Test',
]);
echo $html;
Loading comments...

Stay in the loop

Subscribe to our newsletter and get the latest cybersecurity news delivered straight to your inbox.

Your data is safe. You can unsubscribe from the newsletter at any time.