30分钟速通,php模板引擎smarty
30分钟速通,php模板引擎smarty分类:?技术文档?php+mysql?标签:
以下内容是是php100的php教程视频第27,28,29,30讲的浓缩,并添加一些smarty中文手册的补充材料,简练明了,如果看不懂的话。。。还是先看下27-30讲的视频较好。
?
smarty配置:
?
include(smarty/smarty.class.php); //调用smarty配置文件,默认在smarty根目录下。$smarty = new smarty(); //新建一个对象$smarty->caching = false; //关闭缓存,有利于测试。如需开启缓存,改为true$smarty->cache_lifetime = 60 //设置缓存存活时间,单位秒,必须把caching=true下才作用$smarty->config_dir = ./configs; //设置配置文件目录,可用$smarty->config_load()方法来调用配置文件$smarty->template_dir = ./templates; //设置模板目录$smarty->compile_dir = ./templates_c; //设置编译目录$smarty->cache_dir = ./caches; //设置缓存目录$smarty->left_delimiter = {; //缓存左边界符$smarty->right_delimiter = }; //缓存右边界符?
?
smarty应用:
?
$smarty->assign(模板变量,值/数组);$smarty->display(模板名称);
例:
index.php的代码:?$value = bluesdog say : learn smarty only 30 minutes?$smarty->assign(content,$value); //进行模板变量替换?$smarty->display(index.htm) //该文件就是模板文件,应该在./templates模板目录下?index.htm的代码:?{if $content ne }?{$content}?{/if}??
以上类似:$smarty->cache_lifetime = n 都是smarty的变量以上类似:$smarty->display(template name) 都是smarty的方法以上类似:{if $content ne }…{/if} 都是smarty的函数?
?
smarty循环:
smarty共有2种循环函数,section循环多维数组,foreach循环一维简单数组。?
section举例(参数name和loop必不可少,name=自定义循环名 loop=用来循环的变量):{section name=s loop=$stu}{$stu[s].name}{sectionelse}无内容{/section}
例:新闻列表循环
index.php代码:include(smarty_inc.php); //smarty配置文件$news[]=array(title=>新闻标题第一条,date=>2009-01-01);$news[]=array(title=>新闻标题第二条,date=>2009-01-02);$news[]=array(title=>新闻标题第三条,date=>2009-01-03);$news[]=array(title=>新闻标题第四条,date=>2009-01-04);$news[]=array(title=>新闻标题第五条,date=>2009-01-05);$row=array(标题,作者,当前页);$smarty->assign(row,$row);$smarty->assign(news,$news);$smarty->display(index.htm);
index.htm代码:{$row[0]} | {$row[1]} | {$row[2]}
{section name=list loop=$news}{$news[list].title}-{$news[list].date}{/section}foreach举例(参数from和item必不可少,item=自定义循环名 from=用来循环的变量)://无键值数组{foreach from=$name item=id}id:{$id}
//有键值数组{foreach key=j item=v from=$name}{$j}:{$v}
{/foreachelse}没有内容了{/foreach}
例:
include(“smarty_inc.php”); //smarty配置文件?$value=array(1,3,5,7);$value2=array(‘a’=>’php’,’b’=>’java’,’c’=>’c++);$smarty->assign(‘name’,$value);$smarty->assign(‘name2’$value2);$smarty->display(“index.html”);
index.html代码:{foreach from=$name item=id}数组:{$id}
{/foreach}{foreach from=$name item=id key=k}数组键值:{$k} 数组内容:{$id}
{/foreach}
?
?
smarty内置函数:
?
(注:这些内置函数都是写在模板文件上的,如index.html,index.tpl。foreach和section也是smarty的内置函数,由于较为重要,所以在上一段单独列出)
?
include多功能使用
{include file=header.html}{include file=d:\www\head.htm} // 可以使用绝对路径{include file=head.html title=bluesdog} // 如果head.html中有{$title},则直接替换成bluesdog
?
if条件语句的使用
{if $name==’ok’}…{else}…{/if}
?
literal用以忽略页面中的javascript的{}
{literal}{/literal}
?
strip 删除代码中的首尾空格和回车,让代码成一行
{strip}一般放在html代码的开始和结束{/strip}
?
?
smarty缓存:
?
smarty缓存的配置
$smarty->cache_dir = /caches/;? //缓存目录$smarty->caching = true; //开启缓存$smarty->cache_lifetime = 60; //缓存时间,单位秒//你可以给不同的模板设置不同的缓存过期时间,一个lifetime,一个display。
?
smarty缓存的使用和清除
$smarty->display(‘cache.htm’,cache_id); //创建带id的缓存?$smarty->clear_all_cache(); //清除所有缓存?$smarty->clear_cache(‘index.htm’); //清除index.htm的缓存?$smarty->clear_cache(‘index.htm’,cache_id); //清除指定id的缓存
?
smarty局部缓存函数(注:insert和blockname都是smarty的内置函数,由于较为重要单独拿出来讲)
insert 函数用于小块不缓存内容例1:index.phpfunction insert_shijian (){?return date(y-m-d h:m:s);}index.htm{insert name=shijian}
例2:include(“smarty_inc.php”); //配置的一个smarty类文件?include(“mysql_inc.php”); //一个操作mysql的类文件?if($_get[id]){$sql=”select * from bluesdog where id=”.$_get[id];$query=$db->query($sql); //mysql类中的方法query=mysql_query()?$row=$db->fetch_array($query);?$db->query(‘update bluesdog set hit=hit+1 where id=’.$_get[id]);}$smarty->assign(‘row’,$row);function insert_hits(){global $row;return $row[2];}$smarty->display(‘index.html’);
index.html标题:{$row[name]}
点击:{$row[hit]}次
//缓存开启时,这部分内容不会即时更新?点击:{insert name=’hits’} //insert就能即时更新了
smarty_block 函数用于大量不缓存的内容{blockname}没有缓存的:{$smarty.now}{/blockname}?
?
smarty两个最重要的方法:
?
display显示模板
display (template [,cache_id [,compile_id]])?cache_id指定一个缓存号。compile_id指定一个编译号。
例:include(smarty.class.php);$smarty = new smarty;$smarty->caching = true;
// is_cached也是smarty的方法之一,用于只有在缓存不存在时才调用数据库if(!$smarty->is_cached(index.tpl)){$address = 245 n 50th;$smarty->assign(name,fred);$smarty->assign(address,$address);?}// 显示输出$smarty->display(index.tpl);
?
fetch 取得输出的内容
fetch (template [,cache_id [,compile_id]])?返回一个模板输出的内容(html代码),而不是像display()那样直接显示出来,常用于静态输出html文件。
例:include(smarty.class.php);$smarty = new smarty;$smarty->caching = true;// 只有在缓存不存在时才调用数据库if(!$smarty->is_cached(index.tpl)){?// dummy up some data?$address = 245 n 50th;?$db_data = array(? city => lincoln,? state => nebraska,? zip = > 68502? );$smarty->assign(name,fred);?$smarty->assign(address,$address);?$smarty->assign($db_data);}// 捕获输出$content = $smarty->fetch(index.tpl);//下面将内容写入至一个静态文件?$file = fopen(index.html,w);//ok, 到这里这个index.html静态页就生成了fwrite($file,$content);$fclose($file);
?
smarty变量操作符:
另外smarty一共有20个变量操作符,个人认为只有处理一些英文文章时才有它们的用武之地,所以不再介绍。
最后,可以用{* 这是一个注释 *} 给smarty的模板文件加上必要的注释
smarty常用功能介绍大致就这些,是不是很简单?看看秒表,可能30分钟还没到。
?
http://www.bluesdog.cn/blog/articles/2010-1/2010181622083.asp