PHP Smarty模板的安装

最近开发中用到了PHP中smarty模板。。作为一个长久以来的前端,开始学习PHP模板。。下面将安装教程分享给大家。。

1. 下载Smarty最新版: http://www.smarty.NET/download

2. libs目录下的文件都是Smarty需要的库文件,只要将该文件夹拷贝到要使用Smarty技术的网站根目录就能够使用Smarty了。为了不和已有的文件夹重名,可以修改文件夹的名字为Smarty。

3.在网站根目录下建立如下的目录结构 来测试Smarty的使用。

----Smarty-learn

--------------------smarty(smarty的库文件)

--------------------templates---cache

------------------------------------config

------------------------------------templates--------index.html(模版文件)

------------------------------------templates_c

--------------------index.PHP

index.html 的内容:

<html>
<title><head>test smarty</head></title>
<body>
{$color}
</body>
</html>

index.php的内容:

<?PHP
//引用类文件?
require 'smarty/Smarty.class.php';
$smarty = new Smarty;
//设置各个目录的路径,这里是安装的重点
$smarty->template_dir="templates/templates";
$smarty->compile_dir="templates/templates_c";
$smarty->config_dir="templates/config";
$smarty->cache_dir="templates/cache";
//smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决
$smarty->caching=false;
$hello = "hello smarty!";
$smarty->assign('hello',$hello);
$bgcolor = "green";
$smarty->assign('color',$bgcolor);
$smarty->display('index.html');
?>

在浏览器输入http://localhost/Smarty-learn/,如果出现green,说明测试成功,Smarty没有任何问题,已经可以使用了。

这样的目录结构并不是必须的,只要在使用Smarty的时候配置好这几个目录的路径,任何目录结构都是可以的。但是建议这样使用,层次清楚,不影响网站其他部分。

原文地址:https://www.cnblogs.com/HanJie0824/p/6433811.html