本文實例講述了Smarty模板簡單配置與使用方法。分享給大家供大家參考,具體如下:
在Smarty目錄中建立templates,templates_c,configs,cache目錄。以便Smarty進行編譯和緩存。
建立smarty_inc.php文件對smarty進行配置如下:
<?phpinclude_once("./smarty/Smarty.class.php"); //包含smarty類文件$smarty = new Smarty(); //建立smarty實例對象$smarty$smarty->caching=false; //開發是不建議開啟緩存$smarty->template_dir="./templates"; //設置模板目錄$smarty->compile_dir="./templates_c"; //設置編譯目錄$smarty->cache_dir="./cache"; //緩存文件夾$smarty->cache_lifetime=60;$smarty->left_delimiter = "<{"; //左定界符$smarty->right_delimiter = "}>"; //右定界符?>
行測試,在根目錄下建立index.php文件:
<?php include("smarty_inc.php");$val= array("丁慶","董丹鳳","情侶");$smarty->assign("name",$val);$smarty->display("index.html");?>
在templates下建立index.html模板:
<html><head><meta http-equiv="Content-Type" content="text/html; charset="utf-8"><title>測試頁面</title></head><body><{foreach from=$name item=x}>數組內容:<{$x}> <br/><{/foreach}></body></html>
好了,大功告成,就是這么簡單。