Smarty模板簡(jiǎn)單配置與使用方法示例

字號(hào):


    本文實(shí)例講述了Smarty模板簡(jiǎn)單配置與使用方法。分享給大家供大家參考,具體如下:
    在Smarty目錄中建立templates,templates_c,configs,cache目錄。以便Smarty進(jìn)行編譯和緩存。
    建立smarty_inc.php文件對(duì)smarty進(jìn)行配置如下:
    <?php
    include_once("./smarty/Smarty.class.php"); //包含smarty類(lèi)文件
    $smarty = new Smarty(); //建立smarty實(shí)例對(duì)象$smarty
    $smarty->caching=false; //開(kāi)發(fā)是不建議開(kāi)啟緩存
    $smarty->template_dir="./templates"; //設(shè)置模板目錄
    $smarty->compile_dir="./templates_c"; //設(shè)置編譯目錄
    $smarty->cache_dir="./cache"; //緩存文件夾
    $smarty->cache_lifetime=60;
    $smarty->left_delimiter = "<{"; //左定界符
    $smarty->right_delimiter = "}>"; //右定界符
    ?>
    行測(cè)試,在根目錄下建立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>測(cè)試頁(yè)面</title>
    </head>
    <body>
    <{foreach from=$name item=x}>
    數(shù)組內(nèi)容:<{$x}> <br/>
    <{/foreach}>
    </body>
    </html>
    好了,大功告成,就是這么簡(jiǎn)單。
    希望本文所述對(duì)大家基于smarty模板的PHP程序設(shè)計(jì)有所幫助。