smarty中的fetch("xx.html")与display("xx.html");的区别

 在Smarty模板函数里面有这样一个方法:fetch("template.htm"),他和display("template.htm");最大的不同就是fetch()是把内容输出给一个变量,而display()是把内容输出给浏览器,这样我们就可以用一个变量来接收fetch()的输出,然后把他写入到文件中去.

function insert_cache($file, $display=false) {

ob_start();

if(file_exists($file)) {   

            include $file;

}else {

          exit();

}

ob_get_contents($file);

if($display) { echo $result}

else { return $result}

}

?>

 <?php

include_once 'init_smarty.php';

$smarty->assign('title','标题');
$smarty->assign('content','内容');

$output = $smarty->fetch('index.html');

echo $output;
// $smarty->display('index.html');
?>

使用fetch函数,你可以将要输出的html赋值给一个变量,可以对里面的数据进行一些输出,再将他输出。
smarty中的display方法,实际上调用的就是fetch,只不过是直接将他显示出来而已,而fetch默认是不显示,返回给一个变量的。
原文地址:https://www.cnblogs.com/feng12345/p/5461707.html