速度:相对于其余模板引擎,速度较快php
编译型:在下次访问模板时直接访问编译文件,再也不进行模板从新编译css
缓存技术:能够将用户最终看到的HTML文件缓存成一个静态HTMLhtml
插件技术:smarty能够自定义插件,插件实际上是一些自定义函数ios
强大的表现逻辑:模板中可使用if/else if/end if/foreach等数组
<?php缓存
//1.引入smarty类app
include 'smarty/libs/Smarty.class.php';框架
//2.实例化smarty对象函数
$smarty = new Smarty();优化
//3.设置相关属性
$smarty->template_dir = "templates"; //模板目录
$smarty->compile_dir = "templates_c"; //编译目录
//修改定界符
$smarty->left_delimiter = '<{';
$smarty->right_delimiter = '}>';
//4.分配数据
$smarty->assign("title", "smarty模板引擎");
$smarty->assign("content", "smarty模板引擎的hello world");
//5.载入视图
$smarty->display('index.html');
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{$title}</title>
</head>
<body>
<h2>{$title}</h2>
<p>{$content}</p>
</body>
</html>
smarty中默认的定界符是{ }
① 任何在定界符以外的内容都是静态的,不会被解析(包括php代码)
② 定界符开始的”{”符号和变量$以前不能有空格,例如{ $title}不会被解析
③ 页面中的css或者js的{ }也会被认为是定界符,发生冲突,处理方式以下:
a ) 能够在{开头的地方加上空格
b ) css和js之外部引入的方式添加
c ) 使用内置函数{literal} {须要解析的css或js} {/literal}
④ 修改定界符,在smarty配置的php页面中输入以下代码:
$smarty->left_delimiter = '<{';
$smarty->right_delimiter = '}>';
在index.html视图页面中使用<*注释内容*>来书写smarty的注释
Smarty中的变量来源主要有如下三种:
① 经过PHP程序中的assign函数分配
② 保留变量
③ 配置变量
php中的8种数据类型:
422阵容
4:四种标量类型;整型、浮点型、字符串、布尔型
2:两种复合类型;数组、对象
2:两种特殊类型,资源和null
分配assign变量:
$user=array(“tom”,”jim”,”jack”);
$smarty->assign(‘user’,$user);
在视图页面中输入{$user[0]},或者点语法{$user.0}可获得相应的值此处为tom
无需在php中分配,直接能够在模板页面中使用的变量。包括php中的超级全局变量,好比:$_GET,$_SERVER,以及smarty自带的一些变量
使用格式:{$smarty.保留变量名}
示例:{$smarty.server.SERVER_NAME}
无需在php程序中分配,但不一样于保留变量,它是经过配置文件配置的
示例以下:建立配置文件config.conf,输入以下内容 ,配置文件必须建立在文件夹名configs的文件夹中
copyright="版权信息" //配置文件中的双引号能够去除
police=备案信息
[nationnality]
name=nationnality
[time]
name=time
php页面调用smarty引擎
<?php
//1.引入smarty类
include 'smarty/libs/Smarty.class.php';
//2.实例化smarty对象
$smarty = new Smarty();
//3.设置相关属性
$smarty->template_dir = "templates";
$smarty->compile_dir = "templates_c";
$smarty->display('config.html');
调用配置信息:
在视图页面中输入以下内容便可调用配置信息
{config_load file="test.conf" section="time"} //引用配置文件,并注明使用time这个部分
<h2>{#copyright#}</h2>
<h2>{$smarty.config.police}</h2>
<h2>{#name#}</h2>
//由于配置文件中有两个name,加载文件已指明为time部分,因此此处输出time
1. {if} {elseif} {else}
每个{if}必须有配对的关闭标签:{/if}
内置函数使用示例:在视图页面中输入以下
{if $iq >= 130}
Tom
{elseif $iq <130 && $iq>=110}
Jack
{elseif $iq<110 && $iq>=90}
Lucy
{else}
Jim
{/if} //配对的关闭标签
2. {foreach}
使用foreach循环输出二维数组示例以下:
{foreach}的属性,主要有下述6个:
a ) @index ,当前数组索引,从0开始计算
b ) @iteration,当前循环的次数,从1开始计算
c ) @first ,首次循环时,该值为true
d ) @last ,循环到最后一次时,该值为true
e ) @total ,总的循环次数,可在foreach内部使用,也能够在循环完成以后使用
f ) @show ,在foreach循环执行完以后,检测循环是否显示数据的判断
使用方法示例以下:以@first为例,给第一个tr添加类名
3. {section}
使用section循环时,注意,section不能用于关联数组,只能用于连续下标的数组(0,1,2,…),对关联数组使用循环,须要使用{foreach}
相关参数以下:
使用方式示例:在视图页面中输入以下
{section name=”item” start=0} 表示从第0项开始循环
<li>{$user[item]}</li> //此处的$user是一个索引数组
{/section}
使用index、first等属性:
使用section能够在一次循环中遍历多个数组:
一般状况,在模板页面中,直接输出php程序中分配过来的变量便可,但也有一些特殊状况,须要对分配过来的变量/保留变量,进行再次处理,smarty提供了变量修饰器
使用示例以下:
{$smarty.now|date_format:"%Y-%m-%d %T"} //使用了smarty的时间修饰器
{$content|truncate:10} //使用限制字符长度的修饰器
{"hello"|str_repeat:10} //重复输出hello10次
……
(1)html_radios
{html_radios name=”names” values=$value output=$outlabs selected=”2”}
视图页面中调用这段代码,至关于建立了对应的<input type=”radio”>提示信息
此处的output就是input的外部的提示信息
Selected=2表示第2个数值被选中
(2)html_checkbox //使用方法与html_radios基本相同
{html_checkbox name=”names” values=$value output=$outlabs selected=”Tom”}
若是要设置默认选中多个选项,以数组的形式设置selected的值便可
(3) html_options (下拉列表)
{html_options name=”names” options=$array selected=3}
不须要设置在select下,option赋值一个关联数组便可,value就是数组的key
(4) cycle (交替循环值)
视图页面示例: //输出隔行添加的class
<tr class=”{cycle values=’one,two,three’}”>
<td>{$v.id}</td>
<td>{$v.name}</td>
<td>{$v.age}</td>
</tr>
放在framework中,意味着smarty是框架的一部分
放在application中,是以第三方的方式引入smarty,(third_party)
此处以第三方引入方式为例:
在application>controllers>home>indexControler.class,php文件中
public function indexAction(){
//引入smarty类
Include APP_PATH.”third_party/smarty/smarty.class.php”
//实例化smarty对象
$smarty = new Smarty();
//设置相关属性
$smarty -> template_dir = CUR_VIEW_PATH . “templates”;
$smarty -> compile_dir = CUR_VIEW_PATH . “templates_c”;
//分配数据
$smarty -> assign(‘cats’,$cats);
$smarty -> assign(‘bestGoods’,$bestGoods);
//载入模板文件
$smarty -> display(‘index.html’);
}
能够将上述(2)中的代码写到基础控制类中,再让其余控制器继承自基础控制价,这样能够实现重复利用
能够将头部html页面提取出来,再引用include内置函数方法将提出的head.html页面注入其余页面中,{include file = “head.html”}
主要的缓存方法分为:数据缓存和文件缓存
smarty的缓存属于文件缓存:生成静态页面
smarty设置缓存:
//开启缓存
$smarty->caching=true;
//设置缓存目录 (须要建立响应的文件夹)
$smarty->cache_dir=CUR_VIEW_PATH.”cache”
//设置缓存有效期
$this->smarty->cache_lifetime=60; (默认有效期为3600,单位秒)
//开启smarty调用模式
$smarty->debugging=true; (能够开启调试页面)
//当前页面是首页
$smarty->assign(‘index’,true);
Smarty提供的判断方法,判断是否缓存:isCached
使用示例以下:
if(!$smarty->isCached(‘index.html’)){ //代表没有缓存
执行代码
}
①标签的缓存控制(nocache属性)
显示时间:{$smarty.now|date_format:’%Y-%m-%d %T’} 有缓存刷新时间不变
显示时间:{$smarty.now|date_format:’%Y-%m-%d %T’ nocache} 去除缓存
②变量的缓存控制(适用于单个变量,分配时第三个参数设为true)
声明变量:$time=date(“Y-m-d H:i:s”);
$smarty->assign(“time1”,$time);
$smarty->assign(“time1”,$time,true); //声明第三参数为true,该变量不缓存
③模板区域的缓存控制({nocache} {/nocache} 适用于一块区域)
在视图页面使用{nocache}内置函数,去除缓存,示例以下:
{nocache}
<h3>{$smarty.now|date_format:’%Y-%m-%d %T’}</h3> //该内容不会缓存
{/nocache}
只须要在载入模板文件时,输入区分的参数便可(url中传递的参数)
$smarty->display(‘goods.html’,$goods_id);
同理判断缓存的时候也须要输入这个参数:
$smarty->isCached(‘goods.html’,$goods_id)
设置缓存组:
$smarty->display(“list.html”,”$size|$brand|$style|$material”)
缓存失效状况:超过有效期、模板页面发生变化、删除缓存文件
//删除首页缓存
$smarty->clearCache(“index.html”);
//删除页面指定参数缓存
$smarty->clearCache(“goods.html”,2);
//删除全部缓存
$smarty->clearAllCache();
删除文件使用的底层方法是unlink()函数