一.Smarty的配置php
include_once("Smarty/Smarty.class.php"); //包含smarty類文件html
$smarty = new Smarty(); //創建smarty實例對象$smarty數組
$smarty->config_dir="Smarty/Config_File.class.php"; // 目錄變量緩存
$smarty->caching=false; //是否使用緩存,項目在調試期間,不建議啓用緩存oop
$smarty->template_dir = "./templates"; //設置模板目錄測試
$smarty->compile_dir = "./templates_c"; //設置編譯目錄調試
$smarty->cache_dir = "./smarty_cache"; //緩存文件夾htm
//----------------------------------------------------對象
//左右邊界符,默認爲{},但實際應用當中容易與JavaScript相沖突ip
//----------------------------------------------------
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
二.Smarty的應用
$smarty->assign("模板變量", "值(數組/變量)");
$smarty->display("模板名稱");
例子:
$smarty->assign("name", "測試"); //進行模板變量替換
$smarty->display("index.htm"); // 該文件就是模板文件,應該在模板目錄下
<html>
<title>{$name}</title>
……
三.Smarty循環
{section name=s loop=$stu}
{$stu[s].name}
{sectionelse}
無內容
{/section}
四.例子:
index.php
<?php
include("smarty_inc.php");
$href = array(array("name"=>"新聞第一條","date"=>"2011-1-16"),array("name"=>"php100","date"=>"2011-1-16"),array("name"=>"新聞第二條","date"=>"2011-1-16"),array("name"=>"新聞第三條","date"=>"2011-1-16"),array("name"=>"新聞第四條","date"=>"2011-1-16"));
$row = array("新聞","內容","詳情");
$name = "測試";
$smarty->assign("href",$href);
$smarty->assign("title",$name);
$smarty->assign("row",$row);
$smarty->display("index.htm");
?>
index.htm
<html>
{section name=shuzu loop=$row}
{$row[shuzu]}
{/section}
<hr>
<b>{$title}</b></br>
{section name=list loop=$href}
<a>
{$href[list].name}-{$href[list].date}
</a></br>
{/section}
</html>