The display
method in Smarty converts the Smarty template to HTML, and then outputs that to the page.
Is there any way that I can convert a Smarty template to HTML, and save the contents of such in a variable?
I want to use Smarty to render an email template, but then obviously send that email to a user.
Yes, there is a fetch()
method for exactly this purpose. It takes all the same parameters as the display()
method and the two are cross-referenced in the documentation.
You can save it into a variable using smarty _compile_source function and output buffering like in
function CompileSmartyTemplate(&$smarty,$template) {
$compiled = '';
$smarty->_compile_source('evaluated template', $template, $compiled);
ob_start();
$smarty->_eval('?>' . $compiled);
$compiled = ob_get_contents();
ob_end_clean();
return $compiled;
}
and then
$html = CompileSmartyTemplate($smarty,$template);