这个是Manual上使用fetch()的例子
获取输出后,再用下面给出的wfile()输出成文件<?php 
include("Smarty.class.php"); 
$smarty = new Smarty; $smarty->caching = true; // only do db calls if cache doesn't exist 
if(!$smarty->is_cached("index.tpl")) {   // dummy up some data 
  $address = "245 N 50th"; 
  $db_data = array( 
               "City" => "Lincoln", 
               "State" => "Nebraska", 
               "Zip" => "68502" 
             );   $smarty->assign("Name","Fred"); 
  $smarty->assign("Address",$address); 
  $smarty->assign($db_data); } // capture the output 
$output = $smarty->fetch("index.tpl"); // do something with $output here 
/**
* 写文件操作
*
* @access public
* @param bool
* @return void
*/
function wfile($file,$content,$mode='w') {
    $oldmask = umask(0);
    $fp = fopen($file, $mode);
    if (!$fp) return false;
    fwrite($fp,$content);
    fclose($fp);
    umask($oldmask);
    return true;
}wfile("index.html",$output)
//echo $output; 
?> 

解决方案 »

  1.   

    require('smarty/Smarty.class.php');
    $t = new Smarty;
    $t->assign("title","Hello World!");
    $content = $t->fetch("templates/index.htm");
    //这里的 fetch() 就是获取输出内容的函数,现在$content变量里面,就是要显示的内容了
    $fp = fopen("html/text.html", "w");
    fwrite($fp, $content);
    fclose($fp);//第二种方法:利用ob系列的函数。这里用到的函数主要是 ob_start(), ob_end_flush(), ob_get_content(),其中ob_start()是打开浏览器缓冲区的意思,打开缓冲后,所有来自PHP程序的非文件头信息均不会发送,而是保存在ob_end_flush().而这里最重要的一个函数,就是ob_get_contents(),这个函数的作用是获取缓冲区的内容,相当于上面的那个fetch(),道理一样的。代码:
    ob_start();
    echo "Hello World!";
    $content = ob_get_contents();//取得php页面输出的全部内容
    $fp = fopen("archives/2005/05/19/0001.html", "w");
    fwrite($fp, $content);
    fclose($fp);