Smaty优点:1.代码分离 2.缓存技术
使用步骤:
1.下载Smaty模板
2.将模板中那个lib文件夹复制到项目中(一般为根目录,并且重命名在此命名为Smarty),
3.配置PHP
1.新建一个Smarty_inc.php文件
2.输入
Include_once("Smarty/Smarty.class.php");//包含Smarty类
$smarty=new Smarty();//实例化Smarty对象
//配置
<table border="1" cellpadding="0" cellspacing="0" valign="top" style="direction:ltr; border-collapse:collapse; border-style:solid; border-color:#A3A3A3; border-width:1pt; margin-left:.7076in"><tbody><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.6611in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-family:Calibri; font-size:10.5pt">$smarty->config_dir="Smaryt/Config_File.class.php";</p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:3.0937in; padding:4pt 4pt 4pt 4pt"><p style="margin:0in; font-family:SimSun; font-size:10.5pt">配置文件目录路径,一般不更改</p></td></tr><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.6611in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-family:Calibri; font-size:10.5pt">$smarty->caching=false;</p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:3.0937in; padding:4pt 4pt 4pt 4pt"><p style="margin:0in; font-family:SimSun; font-size:10.5pt">是否开启缓存</p></td></tr><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.6611in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-family:Calibri; font-size:10.5pt">$smarty->template_dir="./templates";</p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:3.0937in; padding:4pt 4pt 4pt 4pt"><p style="margin:0in; font-size:10.5pt"><span lang="zh-CN" style="font-family:SimSun">设置模板目录,手动创建,</span><span lang="en-US" style="font-family:Calibri">./</span><span lang="zh-CN" style="font-family:SimSun">代表当前目录</span><span lang="en-US" style="font-family:Calibri"/><span lang="zh-CN" style="font-family:SimSun; color:red">必须设置</span></p></td></tr><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.6611in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-size:10.5pt"><span style="font-family:Calibri">$samrty->complie_dir="./templates</span><span style="font-family:SimSun">_c</span><span style="font-family:Calibri">";</span></p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:3.0937in; padding:4pt 4pt 4pt 4pt"><p style="margin:0in; font-size:10.5pt"><span lang="zh-CN" style="font-family:SimSun">设置编译目录,手动创建,</span><span lang="en-US" style="font-family:Calibri; color:red"/><span lang="zh-CN" style="font-family:SimSun; color:red">必须设置</span></p></td></tr><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.6611in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin-top:0in; margin-right:0in; margin-bottom:0in; margin-left:0in; font-family:Calibri; font-size:10.5pt">$smarty->cache_dir="./smarty_cache";</p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:3.0937in; padding:4pt 4pt 4pt 4pt"><p style="margin-top:0in; margin-right:0in; margin-bottom:0in; margin-left:0in; font-family:SimSun; font-size:10.5pt">设置缓存目录</p></td></tr><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.6611in; padding:4pt 4pt 4pt 4pt"><p style="margin-top:0in; margin-right:0in; margin-bottom:0in; margin-left:0in; font-size:10.5pt"><span lang="en-US" style="font-family:Calibri">$smarty->left_delimiter="{";</span><span lang="zh-CN" style="font-family:SimSun">和</span><span lang="en-US" style="font-family:Calibri">$smarty->right_delimiter="}";</span></p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:3.0937in; padding:4pt 4pt 4pt 4pt"><p style="margin:0in; font-size:10.5pt"><span lang="zh-CN" style="font-family:SimSun">设置在</span><span lang="en-US" style="font-family:Calibri">HTML</span><span lang="zh-CN" style="font-family:SimSun">标志</span><span lang="en-US" style="font-family:Calibri">Smarty</span><span lang="zh-CN" style="font-family:SimSun">的开始和结束标签</span><span lang="en-US" style="font-family:Calibri"/><span lang="zh-CN" style="font-family:SimSun; color:red">自定义</span><span lang="en-US" style="font-family:Calibri; color:red"/><span lang="zh-CN" style="font-family:SimSun; color:red">也可以世</span><span lang="en-US" style="font-family:Calibri; color:red">{%</span><span lang="zh-CN" style="font-family:SimSun; color:red">和</span><span lang="en-US" style="font-family:Calibri; color:red">%}</span></p></td></tr></tbody></table>
Smarty使用变量
定义变量
$smarty->assign("模板变量","值/数组");
将改变量应用到特定的模板中
$smarty->display("模板名");
Eg:
~~~
$smarty->assign("title","我的第一个smarty程序");
$smarty->display("index.html");
在index.html中使用
<html>
<head>
<title>{$title}</title>
</head>
</html>
~~~
上述一次只能访问一个变量那么**怎样循环访问多个变量呢**?**
要想访问多个变量必须用到数组
定义
~~~
$array={name->"jiangtong",name->"张三",name->"李四"}
$smarty->assign($student,$name);
$smarty->display("index.html");
~~~
**在**index.html**中使用**
<html>
<head></head>
<body>、
<table border="1" cellpadding="0" cellspacing="0" valign="top" style="direction:ltr; border-collapse:collapse; border-style:solid; border-color:#A3A3A3; border-width:1pt; margin-left:.3333in"><tbody><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.7409in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-family:Calibri; font-size:10.5pt">{section name="stuList" loop="$student"}</p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:1.6715in; padding:4pt 4pt 4pt 4pt"><p style="margin:0in; font-size:10.5pt"><span lang="en-US" style="font-family:Calibri">loop</span><span lang="zh-CN" style="font-family:SimSun">代表循环那个数组</span></p></td></tr><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.7409in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-size:10.5pt"><span style="font-family:Calibri"><p>{$student</span><span style="font-family:SimSun">[</span><span style="font-family:Calibri">stuList</span><span style="font-family:SimSun">].name</span><span style="font-family:Calibri">}</p></span></p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:1.6715in; padding:4pt 4pt 4pt 4pt"><p style="margin:0in; font-family:SimSun; font-size:10.5pt"> </p></td></tr><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.7409in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-family:Calibri; font-size:10.5pt"><sectionelse></p><p style="margin:0in; font-size:10.5pt"><span lang="en-US" style="font-family:Calibri">//</span><span lang="zh-CN" style="font-family:SimSun">可以不写</span></p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:1.6715in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-family:Calibri; font-size:10.5pt"> </p></td></tr><tr><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:2.7409in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-family:Calibri; font-size:10.5pt">{/section}</p></td><td style="border-style:solid; border-color:#A3A3A3; border-width:1pt; vertical-align:top; width:1.6715in; padding:4pt 4pt 4pt 4pt"><p lang="en-US" style="margin:0in; font-family:Calibri; font-size:10.5pt"> </p></td></tr></tbody></table>
<body>
</html>
Smarty取出当前时间
{$smarty.now}