php的模板引擎中,Smarty就是藍翔挖掘機專業。
安裝:
(1)下載Smarty
(2)解壓複製libs到你的網站根目錄,如htdocs/smarty/libs,想改其他名字也可
(3)在smarty文件夾下建立所需的各種文件
cache 緩存目錄
configs 配置參數目錄
templates 模板目錄,如index.tpl
templates_c 模板與腳本編譯文件目錄,實際上用戶訪問的文件
index.php 用戶腳本文件,可自己命名
1、第一種安裝方式------------直接在index.php文件裏引用安裝
在index.php里加入以上代碼
require_once 'libs/Smarty.class.php';
$smarty = new Smarty();
$smarty->assign("hello", "hello,world");
$smarty->display('index.tpl');
在templates裏建立文件index.tpl,名字可自定義,內容
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Smarty的安裝使用</title>
</head>
<body>
<h1>{$hello}</h1>
</body>
</html>
執行localhost/smarty/index.php,可看見hello,world的輸出
2、第二種安裝方式------------在smarty目錄下添加配置文件smarty_inc.php
<?php
require_once 'libs/Smarty.class.php';
$smarty = new Smarty();
?>
在index.php文件裏引用
require_once 'smarty_inc.php';
$smarty->assign("hello", "hello,world");
$smarty->display('index.tpl');
在templates裏建立文件index.tpl,名字可自定義,內容
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Smarty的安裝使用</title>
</head>
<body>
<h1>{$hello}</h1>
</body>
</html>
執行localhost/smarty/index.php,同樣可看見hello,world的輸出
3、第三種安裝方式------------在smarty目錄下添加配置文件smarty_ini.php
<?php
require_once 'libs/Smarty.class.php';
class mySmarty extends Smarty {
function __construct()
{
// Class Constructor.
// These automatically get set with each new instance.
parent::__construct();
$this->setTemplateDir('templates/');
$this->setCompileDir('templates_c/');
$this->setConfigDir('configs/');
$this->setCacheDir('cache/');
}
}
?>
在index.php文件裏引用
require_once 'smarty_ini.php';
$smarty = new mySmarty();
$smarty->assign("hello", "hello,world");
$smarty->display('index.tpl');
在templates裏建立文件index.tpl,名字可自定義,內容
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Smarty的安裝使用</title>
</head>
<body>
<h1>{$hello}</h1>
</body>
</html>
執行localhost/smarty/index.php,同樣可看見hello,world的輸出