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.

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

Easy:

php
$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;

Newsletter

Nejlepsi tipy a triky o PHP do Vaseho e-mailu. Clanky a novinky nejen ze sveta PHP a programovani.