1.从smarty官网上下载最新的版本文件 : https://www.smarty.net/download
我目前下载的最新版本是3.1.33
2.在自己网站根目录创建一个smarty文件夹,将下载的压缩包解压,复制 libs 文件夹到该目录下
libs为smarty的核心文件无需更改,demo为示例,事后可以看一下(一下是文件夹的目录)
3.现在我们在smarty(核心文件)的同级目录创建一个test文件夹 文件夹下创建test.php文件 (内容如下:)
<?php
//smarty引入
require('../smarty/Smarty.class.php'); //对比自己核心文件目录(我的目录是smarty)
//实例化
$smarty = new Smarty();
$smarty->left_delimiter = "{"; //左定界符
$smarty->right_delimiter = "}"; //右定界符
$smarty->template_dir = "templates/templates"; //html模板地址
$smarty->compile_dir = "templates/templates_c"; //模板编译生成文件
$smarty->config_dir = "templates/config"; //模板配置
$smarty->cache_dir = "templates/cache"; //缓存
//smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决
//$smarty->caching = false;
//$smarty->cache_lifettime = 120; //缓存时间
$smarty->assign('title','文章标题');
//指向文件
$smarty->display('../template/templates/test.tpl');
?>
根据test.php在smarty同级下创建template文件夹 template文件夹下有该文件夹
在templates下创建一个测试文件 名为test.tpl文件 (文件后缀随意命名,我这儿定义后缀为tpl,代表是模板文件)
{$title} <br/>
test.tpl 文件 写入 {$title} 为模板引擎定义的输出方式 { }为左右定界符
运行文件输出文章标题
自此smarty初步使用已完成,快去试一下吧!