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

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

问题描述:

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.

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

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

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

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