Smarty 是什么: 我的理解就是一个框架,便于分层,代码分离...(详细去问谷歌)
下载地址:http://www.smarty.net/download
下载后只需要将里面的 “libs”文件夹COPY出来,放到网站目录下(可以改名,我改名就叫Smarty ),其它不用去管。OK 开始我们的配置使用 Smarty 。
一:在 网站目录下建立一个可以引用配置 smarty 的文件,我建立文件 为 “smarty_conn.php” ,该文件配置如下:
include_once("Smarty/Smarty.class.php"); //引用smarty中类文件 $smarty=new Smarty(); //实例化 $smarty->caching=false; //缓存设置 $smarty->template_dir="./templates"; //模板目录,html $smarty->compile_dir="./templates_c"; //编译目录 $smarty->cache_dir="./smarty_cache"; //缓存目录 // 设置html代码中使用的边界符号 $smarty->left_delimiter="{"; $smarty->right_delimiter="}";
二:建立必须的文件夹。根据上面的“smarty_conn.php”配置文件,在指定目录建立必须的文件夹
1:templates 模板目录,存放html页面
2:templates_c 编译目录,建立好后,不用去管它了
3:smarty_cache 缓存目录,一般在开发调试过程中,没启用缓存,这个也不用去管它
三:OK,简单配置就是以上这些了,下面给个小实例
1:在网站目录下建立一个 "index.php"的文件,同时也在模板templates 目录下建立 一个 index.php 或 index.html文件
附图:
网站下的 index.php 代码:
include("smarty_conn.php"); //得先引用我们配置好的smarty文件 $name="hello"; $smarty->assign("title",$name); //这里我也不知道怎么说,看模板文件怎么调用就明白了 $smarty->display("index.php"); //指定的模板文件
2:模板templates 目录下的 index.php 代码
<html> 你好{$title} </html>
运行结果是 你好hello
还有一种是用数组的形式,吧值放在页面中
网站下的 index.php 代码:
$row=array("1","haozi","voip"); $smarty->assign("row",$row); $smarty->display("index.php");
模板templates 目录下的 index.php 代码:
<html> {$row[0]},{$row[1]},{$row[2]} </html>
输出结果为 :1,haozi,voip
OK ,作为自己的学习笔记,仅供参考
