分类:?技术文档?PHP+MySql?标签: 以下内容是是PHP100的PHP教程视频第27,28,29,30讲的浓缩,并添加一些smarty中文手册的补充材料,简练明了,如果看不懂的话。。。还是先看下27-30讲的视频较好。 ? ? ? ? ? $smarty->assign("模板变量","值/数组"); 例: index.php的代码:? ? ? ? section举例(参数name和loop必不可少,name=自定义循环名 loop=用来循环的变量): 例:新闻列表循环 index.php代码: index.htm代码: foreach举例(参数from和item必不可少,item=自定义循环名 from=用来循环的变量): //有键值数组 例: include(“smarty_inc.php”); //smarty配置文件? index.html代码: ? ? (注:这些内置函数都是写在模板文件上的,如index.html,index.tpl。foreach和section也是smarty的内置函数,由于较为重要,所以在上一段单独列出) ? include多功能使用 {include file="header.html"} ? IF条件语句的使用 {if $name==’ok’} ? literal用以忽略页面中的javascript的{} {literal} ? strip 删除代码中的首尾空格和回车,让代码成一行 {strip} ? ? ? smarty缓存的配置 $smarty->cache_dir = "/caches/";? //缓存目录 ? smarty缓存的使用和清除 $smarty->display(‘cache.htm’,cache_id); //创建带ID的缓存? ? smarty局部缓存函数 include(“smarty_inc.php”); //配置的一个smarty类文件? index.html ? ? ? display (template [,cache_id [,compile_id]])? 例: // is_cached也是smarty的方法之一,用于只有在缓存不存在时才调用数据库 ? fetch 取得输出的内容 fetch (template [,cache_id [,compile_id]])? 例: ? smarty变量操作符: 另外smarty一共有20个变量操作符,个人认为只有处理一些英文文章时才有它们的用武之地,所以不再介绍。 最后,可以用{* 这是一个注释 *} 给smarty的模板文件加上必要的注释 Smarty常用功能介绍大致就这些,是不是很简单?看看秒表,可能30分钟还没到。 ? http://www.bluesdog.cn/blog/articles/2010-1/2010181622083.asp30分钟速通,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->display("模板名称");
$value = "bluesdog say : learn smarty only 30 minutes"?
$smarty->assign("content",$value); //进行模板变量替换?
$smarty->display("index.htm") //该文件就是模板文件,应该在./templates模板目录下?
index.htm的代码:?
<HTML>
{if $content ne ""}?
{$content}?
{/if}?
</HTML>?
smarty循环:
smarty共有2种循环函数,section循环多维数组,foreach循环一维简单数组。
{section name=s loop=$stu}
{$stu[s].name}
{sectionelse}
无内容
{/section}
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");
<html>
{$row[0]} | {$row[1]} | {$row[2]}
<hr>
<ul>
{section name=list loop=$news}
<li>
{$news[list].title}-{$news[list].date}
</li>
{/section}
</ul>
</html>
//无键值数组
{foreach from=$name item=id}
id:{$id}<br>
</foreach>
{foreach key=j item=v from=$name}
{$j}:{$v}<br>
{/foreachelse}
没有内容了
{/foreach}
$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”);
{foreach from=$name item=id}
数组:{$id}<br>
{/foreach}
{foreach from=$name item=id key=k}
数组键值:{$k} 数组内容:{$id}<br>
{/foreach}
?
smarty内置函数:
{include file="D:\www\head.htm"} // 可以使用绝对路径
{include file="head.html" title="bluesdog"} // 如果head.html中有{$title},则直接替换成bluesdog
…
{else}
…
{/if}
<script language=javascript>
fun1(){
… …
}
</script>
{/literal}
一般放在html代码的开始和结束
{/strip}
smarty缓存:
$smarty->caching = true;?????????? //开启缓存
$smarty->cache_lifetime = 60;???? //缓存时间,单位秒
//你可以给不同的模板设置不同的缓存过期时间,一个lifetime,一个display。
$smarty->clear_all_cache(); //清除所有缓存?
$smarty->clear_cache(‘index.htm’); //清除index.htm的缓存?
$smarty->clear_cache(‘index.htm’,cache_id); //清除指定id的缓存
(注:insert和blockname都是smarty的内置函数,由于较为重要单独拿出来讲)
例1:
index.php
function insert_shijian (){
?return date("Y-m-d H:m:s");
}
index.htm
<div>{insert name="shijian"}</div>
例2:
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’);
标题:{$row[name]}<br>
点击:{$row[hit]}次<br> //缓存开启时,这部分内容不会即时更新?
点击:{insert name=’hits’} //insert就能即时更新了
{blockname}
没有缓存的:{$smarty.now}
{/blockname}
smarty两个最重要的方法:
display显示模板
cache_id指定一个缓存号。
compile_id指定一个编译号。
include("Smarty.class.php");
$smarty = new Smarty;
$smarty->caching = true;
if(!$smarty->is_cached("index.tpl")){
$address = "245 N 50th";
$smarty->assign("Name","Fred");
$smarty->assign("Address",$address);?
}
// 显示输出
$smarty->display("index.tpl");
返回一个模板输出的内容(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);
详细解决方案
30分钟速通,PHP模板发动机Smarty
热度:1193 发布时间:2012-12-19 14:13:14.0
相关解决方案
- zend framework2 集成 smarty 文件
- smarty 数据库查询有关问题
- Smarty 的控制器怎么调用其他文件的方法
- smarty section,该如何处理
- smarty 模板路径有关问题
- smarty 模版中,js修改了表单的值,如其直接在模版中用该表单值进行if语句判断
- smarty 遍历多维数组有关问题
- smarty 揭示403
- smarty 截取字符串,调用php中的步骤,foreach循环
- smarty 如何在模板上循环遍历输出三维数据?
- smarty tpl 循环,该怎么解决
- smarty 模板编辑器,该怎么解决
- smarty 如若给模板传值是动态的,生成缓存 也是动态
- smarty 怎么实现i++
- smarty 循环,该怎么解决
- PHP(三): Start using Smarty
- smarty 截取字符串解决方法
- php+smarty 为什么在IE上正常,在火狐+IE9上全是源代码?
- smarty 新手
- smarty 在include file 包含动态内容的时候报错(http 500),该如何处理
- smarty 和 iframe 的有关问题
- smarty 页面空缺
- php smarty 缓存施用
- php smarty foreach name index first last etc
- smarty 截取字符串解决思路
- smarty options汉语言显示不正常
- smarty,该如何解决
- Fatal error: Smarty error:syntax error: unrecognized tag,该怎么解决
- smarty 怎么调用 php类的常量成员
- smarty 在include file 包孕动态内容的时候报错(http 500)