smarty簡單模板變量輸出方法

字號:


    本文簡單講述了Smarty的模板變量輸出方法。分享給大家供大家參考,具體如下:
    config.inc.php配置文件如下:
    include_once('smarty/Smarty.class.php');
    $smarty=new Smarty();
    $smarty->config_dir="smarty/Config_File.class.php";
    $smarty->caching=false; //是否開啟緩存
    $smarty->template_dir="./templates"; //模板目錄
    $smarty->compile_dir="./templates_c"; //編譯目錄
    $smarty->cache_dir="./smarty_cache"; //緩存目錄
    $smarty->left_delimiter="<{";
    $smarty->right_delimiter="}>";
    index.php文件如下:
    include('./config.inc.php');
    $title="php測試標題";
    $content="smarty練習(xí)!";
    $newstext[]=array("name"=>"php程序測試","date"=>"2015-08-01");
    $newstext[]=array("name"=>"php視頻教程","date"=>"2015-08-02");
    $newstext[]=array("name"=>"smarty學(xué)習(xí)","date"=>"2015-08-03");
    $row=array("標題","姓名","年齡");
    $smarty->assign("title",$title);
    $smarty->assign("row",$row);//一個數(shù)組傳遞多個值
    $smarty->assign("shownewstext",$newstext);//二維數(shù)組傳遞多個值
    $smarty->assign("content",$content);
    $smarty->display('index.htm');
    運行結(jié)果如下:
    測試數(shù)組:標題 | 姓名 | 年齡 
    --------------------------------------------------------------------------------
    測試內(nèi)容為:smarty練習(xí)! 
    循環(huán)新聞內(nèi)容為:
    php程序測試 - 2015-08-01
    php視頻教程 - 2015-08-02
    smarty學(xué)習(xí) - 2015-08-03
    希望本文所述對大家基于Smarty模板的php程序設(shè)計有一定幫助作用。