Smarty fetch方法


fetch
取得輸出的內容

string fetch (string template [, string cache_id [, string compile_id]])

 

返回一個模板輸出的內容(HTML程式碼),而不是直接顯示出來,需要指定一個合法的模 板資源的型別和路徑。你還可以通過 第二個可選引數指定一個快取號,相關的資訊可以檢視快取。

 

通過第三個可選引數,可以指定一個編譯號。這在你想把一個模板編譯成不同版本時使用,比如針對不同的語言編譯模板。編譯號的另外一個作用是,如果你 有多個$template_dir模板目錄,但只有一個$compile_dir編譯後存檔目錄,這時可以為每一個$template_dir模板目錄指 定一個編譯號,以避免相同的模板檔案在編譯後會互相覆蓋。相對於在每一次呼叫display()的時候都指定編譯號,也可以通過設定$compile_id編 譯號屬性來一次性設定。


Example 13-14. fetch
例子 13-14. 取得輸出的內容

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
// 對將要輸出的內容進行處理


echo $output;