Smarty安裝教程

來源:互聯網
上載者:User

smarty的最新網址:http://www.smarty.net/

到這個頁面http://www.smarty.net/download.php
 下載最新的smarty源檔案。

解壓下載的檔案,把libs檔案夾及目錄下的所有檔案拷貝到的一新的檔案夾如:smarty_temp目錄下面,然後
再在這個目錄下面建立立四個檔案夾:templates,templates_c,cache,configs

然後再建立立一個設定檔:config.inc.php,內容如下:

<?php

/*
 * 只要把此檔案放在和templates,templates_c,cache,configs同一個目錄下,
 * 在其它地方只要調用這個檔案,就可以顯示出來了。
 *
 */
    // 把D:/phpweb/myweb/smarty_temp路徑換成D:/phpweb/myweb/smarty_temp
    $PathArray = explode('smarty_temp',str_replace('//','/',dirname(__FILE__)));
   
    define('ROOT',$PathArray[0]);                    //設定網站檔案的絕對路徑
    define('SMARTY_ROOT',ROOT.'smarty_temp/');        //設定Smarty類庫路徑
   
    require_once 'libs/Smarty.class.php';
    $Smarty = new Smarty();                        //建立新的Smarty對象
    $Smarty -> compile_check   = TRUE;            //每次PHP執行時查看模板檔案的內容是否改變。
    //debugging控制台,這是一個切換參數,如果開啟它將顯示Smarty變數和運行狀態的調試視窗。
    $Smarty -> debugging       = FALSE;
   
    //設定模板目錄
    $Smarty -> template_dir    = SMARTY_ROOT.'templates/';
   
    //Smarty預設的模板目錄名:templates_c
    $Smarty -> compile_dir     = SMARTY_ROOT.'templates_c/';
   
    //預設範本緩衝的目錄名
    $Smarty -> cache_dir       = SMARTY_ROOT.'cache/';
   
    //預設的config檔案目錄名:configs
    $Smarty -> config_dir      = SMARTY_ROOT.'configs/';
   
    //左右邊界符,預設為{},但實際應用當中容易與JavaScript相衝突,所以建議設成<{}>或其它。
    $Smarty -> left_delimiter  = '<{';
    $Smarty -> right_delimiter = '}>';

?>

 

測試一下:

在tmeplates目錄下建立test.html模板檔案,內容如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><{$title}></title>
</head>
<body>
<{$content}>
</body>
</html>

然後在smarty_temp目錄下建立test.php檔案,內容如下:

<?php
    require_once 'Config.inc.php';//只要能把這個檔案包含進來,test.php檔案放在哪都可以。
   
    $Smarty->assign("title", "測試成功了,這是標題"); //進行模板變數替換
    $Smarty->assign("content", "測試結果成功"); //進行模板變數替換
    $Smarty->display('test.htm');//編譯並顯示位於./templates下的index.tpl模板
?>

運行一下就可以了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.