要用smarty模板引擎,首先要下載smarty,筆者在百度網盤上存的smarty3.1.28,[點此下載](https://pan.baidu.com/s/1boDE5n5)。
下載解壓,其中有一個libs目錄,將此目錄里的文件復制到www/cart/smarty目錄里(在www里建立smarty目錄)。
另外,必須cart根目錄再建4個目錄, 這4個目錄是使用smarty引擎必須的,分別是templates、 templates_c、configs、cache。
templates:用于存放模板文件。
templates_c:用于存放編譯后的模板文件。
configs:用于存放配置文件。
cache:模板文件緩存目錄。
* * * * *
在www/cart根目錄建一個config.php文件,其中代碼如下:
~~~
<?php
//加載smarty核心文件.
require ('smarty/Smarty.class.php');
//實例化一個Smart對象.
$smarty=new Smarty;
//指定模板文件的存儲位置.
$smarty->template_dir='templates';
//指定模板編譯文件存儲位置.
$smarty->compile_dir='templates_c';
//指定配置文件存儲位置.
$smarty->config_dir='configs';
//指定緩存文件存儲位置.
$smarty->cache_dir='cache';
?>
~~~
因4個目錄與smarty核心目錄都在根目錄下,所以配置代碼簡單。
* * * * *
測試smarty配置是否成功。
1、在www/cart/建立index.php
*養成寫一段代碼,就進行測試的習慣,這樣不致于寫幾十行代碼,運行時出錯,很郁悶!尤其是標點錯或某個函數中了一個字符錯,錯不大,難找。*
代碼及注釋。
~~~
<?php
//導入smarty核心文件.
require_once('config.php');
//定義變量
$title='這是測試標題';
$content='這是測試內容';
//將變量賦值模板中的變量
$smarty->assign('title',$title);
$smarty->assign('content',$content);
//調用模板文件.
$smarty->display('index.html');
?>
~~~
2、在www/cart/templates中建立index.html.
代碼如下:
~~~
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!--模板變量title,它接收index.php中傳過來的值-->
<title>{$title}</title>
<!--bootstrap.min.css樣式鏈接-->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<hr />
<!--模板變量content-->
<h2> {$content}</h2>
<hr />
</body>
</html>
~~~
bootstrap.min.css,用此樣式表,使菜鳥也能編出優雅、大氣的網頁。使用網上的鏈接為了測試方便。
運行測試:http://localhost/cart/index.php
效果圖:

說明smarty模板配置成功,下一步該進行數據庫連接文件設置了。
* * * * *
## 天行健,君子當自強不息!
* * * * *