Smarty是一个使用PHP写出来的模板引擎,目的是分离php代码和html代码。是一个编译型模板引擎。
安装
(1)下载Smarty,
(2)在web服务器文档根目录新建一个smarty/文件夹,
(3)把解压Smarty后lib文件夹里的文件拷到smarty/下,
(4)在smarty/下新建4个文件夹template/自已的模板文件,templates_c/编译时生成的文件,cache/缓存的文件,configs/配置文件。
(5)编码指定(4)中的4个文件夹路径。在文档根目录下新建文件smart.php
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty();//设置各个目录的路径,这里是安装的重点
$smarty->template_dir ="smarty/libs/templates/templates";
$smarty->compile_dir ="smarty/libs/templates/templates_c";
$smarty->config_dir = "smarty/libs/templates/config";
$smarty->cache_dir ="smarty/libs/templates/cache";
?>
使用
(1)在文档根目录/smarty/template/下新建模板文件index.tpl
smarty demoIt's as easy as {$test}.
(2)在文档根目录/新建php文件index.php
require_once("smarty.php");
$smarty->assign('test', 123);
$smarty->display('index.tpl');
?>(3)通过浏览器来访问index.php,http://localhost/index.php就会显示:
It's sa easy as 123.