进入smarty的解压目录,把其中的libs目录拷贝到php服务目录下
cd smarty-2.6.28
cp -r libs /var/www/html/libs
在php服务目录下执行以下新建Mysmarty/cache(缓存目录)、Mysmarty/configs(配置目录)、Mysmarty/templates(模板目录)、Mysmarty/templates_c(模板编译目录),注意把cache,templates_c对用户权限修改成可读写
mkdir Mysmarty
cd Mysmarty
mkdir cache configs templates templates_c
chmod 777 cache //更改成全权限,在测试使用中可这样做
chmod 777 templates_c
在templates目录下新建模板文件test.tpl
cd templates
vi test.tpl
编辑test.tpl内容:
在Mysmarty目录下新建调用文件test.php
cd .. //回到Mysmarty目录
vi test.php
编辑test.php内容:
include_once("/var/www/html/libs/Smarty.class.php"); //包含smarty类文件
$smarty=new Smarty(); //建立smarty实例对象$smarty
$smarty->template_dir='/var/www/html/Mysmarty/templates'; //设置模板目录
$smarty->compile_dir='/var/www/html/Mysmarty/templates_c'; //设置模板编译生成文件目录
$smarty->config_dir='/var/www/html/Mysmarty/configs';
$smarty->cache_dir='/var/www/html/Mysmarty/cache'; //设置缓存目录
$smarty->cache_lifetime=30; //缓存时间
$smarty->caching=true; //true开启缓存,false关闭缓存
$smarty->left_delimiter="
$smarty->right_delimiter="}>";
$smarty->assign("title","test");
$smarty->assign("content","hello smarty.");
$smarty->display("test.tpl");
?>
浏览器访问验证
浏览器下访问域名+/Mysmarty/test.php,我自己的的路径是http://192.16.137.2/Mysmarty/test.php, 看到结果如下,就OK了。