是否可以将Smarty模板转换为HTML而不输出到页面?

Is it possible to convert Smarty template to HTML without outputting it to the page?

Smarty中的display方法将Smarty模板转换为HTML,然后输出到页面。

有什么方法可以将 Smarty 模板转换为 HTML,并将其内容保存在变量中?

我想使用 Smarty 呈现电子邮件模板,但显然会将电子邮件发送给用户。

是的,有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

您可以使用 smarty _compile_source 函数和输出缓冲将其保存到变量中,如

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;   
}

然后是

$html = CompileSmartyTemplate($smarty,$template);