PHP Manual
/
Processing of chains

How to render a Latte template to a string

18. 12. 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;

Jan Barášek   Více o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Související články

1.
8.
Status:
All systems normal.
2024