系统 Ubuntu 18
smarty可以在用php写后端时配置网页模板, 使网页样式和数据分离,
安装
首先在smarty官网下载压缩包,下载完后是 .tar.gz文件,分别用gzip和tar解压并提取,
gunzip [filename]
得到file1
tar -x [file1]
解压缩完成得到几个文件,我们只需要libs/
目录下的东西,可以改名为smarty/
,放到你喜欢的目录下。由于它只是php编写的库,稍后我们只要引入就可以了。
使用
在网站根目录下建立cache configs templates templates_c
四个目录,给与templates/
和 templates_c/
775权限,
在你喜欢的地方建立index.php
,
输入,
<?php
require_once('../libs/smarty/Smarty.class.php'); #临时引用smarty库
$smarty = new Smarty(); #建立新Smarty实例
#设置配置目录路径
$smarty->setTemplateDir('../templates/');
$smarty->setCompileDir('../templates_c/');
$smarty->setConfigDir('../configs/');
$smarty->setCacheDir('../cache/');
$smarty -> assign('test','OK'); #指定变量的值(var,value)
$smarty->display('test.html'); #选择要显示的模板
?>
将要应用的模板test.html
放到templates/
下,试了下可支持.php,.html和.tpl
test.html
测试内容,
{$test}
{
和}
是默认的边界符,可以在配置中自定义。实测发现{$test}
等价于{{$test}}
和{{{$test}}}
。
运行index.php
,显示测试内容,
OK
完成。