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

时间:2016-06-27 11:22:36

标签: php html email smarty html-email

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

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

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

2 个答案:

答案 0 :(得分:4)

是的,正是为了这个目的,a fetch() method。它采用与the display() method相同的所有参数,并且这两个参数在the documentation中交叉引用。

答案 1 :(得分:1)

您可以使用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);