Smarty if else /if判斷 if语句和和条件同php差不多, 但每个词之间必须用空格分割开。 也有一些新的条件语句, 列举如下: eq相等, ne、neq不相等, gt大于, lt小于, gte、ge大于等于, lte、le 小于等于, not非, mod求模。 is [not] div by是否能被某数整除, is [not] even是否为偶数, $a is [not] even by $b即($a / $b) % 2 == 0, is [not] odd是否为奇, $a is not odd by $b即($a / $b) % 2 != 0 示例:
{if $name eq "Peter"} Welcome male. {elseif $name eq "Fengfeng"} Welcome female. {else} Welcome, whatever you are. {/if} 换行 {*section name=loop loop=$daili*}<td height="24" align="center" class="zixun_font2"><a href="html/{*$daili[loop].filename*}" target="_blank">{*$daili[loop].title*}</a><font color="#999999" size="1">[{*$daili[loop].addtime|date_format:"%m.%d"*}]</font></td>{*if $smarty.section.loop.rownum is div by 4*}</tr><tr></tr>{*/if*} //这行代理是换行的意思{*/section*}<!--{foreach from=$pd1 item=p key=i name=x}--> <td><table border="0" cellpadding="0" cellspacing="0"> <tr><td><a href="<!--{$p.ProductPhoto2}-->" onclick="return hs.expand(this)"><img width="230" height="150" border="0" title="<!--{$Html.ClickEnlarge}-->" src="<!--{$p.ProductPhoto1}-->" /></a></td></tr><tr><td align="center" height="25"><!--{* 截取指定长度的名称并将搜索字替换成红色 *}--> <a target="_blank" href="?File=product_detail&Lang=<!--{$Sys.Lang}-->&ID=<!--{$p.ID}-->"><!--{if $p.ProductNameColor == ""}--> 产品名称:<!--{$p.ProductName|SongTruncate:6|replace:$smarty.get.wd:$pt.Redwd}--> <!--{else}--> <font color="<!--{$p.ProductNameColor}-->"><!--{$p.ProductName|SongTruncate:6|replace:$smarty.get.wd:$pt.Redwd}--></font><!--{/if}--> </a></td></tr><tr><td align="center" height="25">产品型号:<!--{$p.ProductModel}--></td></tr><tr><td> </td></tr></table></td><!--{if $smarty.foreach.x.iteration is div by 2}--></tr><tr><!--{/if}--> <!--{foreachelse}--><td height="100"><!--{$Html.NotProduct}--></td><!--{* 没有数据时显示 *}--> <!--{/foreach}--> Smarty中批量生成html的方法 经过一番摸索,终于找到方法,写出来大家指点一下:首先在smartt.class.php中为Smarty类添加这么一个方法:PHP代码://参数一:html文件保存路径,参数2:写入的内容 以下为引用的内容:function MakeHtmlFile($file_name, $c) { if(!$fp = fopen($file_name, "wa")) { echo "文件打开失败!"; return false; } if(!fwrite($fp, $c)) { echo "文件写入失败!"; fclose($fp); return false; } fclose($fp); } 问题:我的所有文章都调用的是news.tpl这个模板,那么究竟如何批量生成呢? 我们先看看news.phpPHP代码: 以下为引用的内容:<?phpinclude_once("config.php");include_once("init.php");$s->assign("title","所有的新闻分类");$ID=$_GET["ID"]+0;$sql="select * from artical where newsID=$ID";$rs=$db->fetch($sql);$s->assign("news",$rs["rec"][0]);//注意:$rs["rec"][0]是个数组$s->display("news.html");?> 整个模板变量就只有$news,这么写有什么好处呢?我可以以数组的方式读取内容 那么究竟如何生成:看如下代码:很简单的PHP代码: 以下为引用的内容:<?phpinclude_once("config.php");include_once("init.php");$sql="select * from artical";$rs=$db->fetch($sql);foreach ($rs["rec"] as $k=>$v){$s->assign("news",$v);$s->MakeHtmlFile("./news/news_".$v[0].".html",$s->fetch("news.html",null, null, false));}?> 很简单把! PHP代码: 以下为引用的内容:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]"><html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title><{$news.titles}></title><link href="<{$docroot}>/CSS/main.css" rel="stylesheet" type="text/css" /></head> <body><table width="800" border="0" align="center" cellpadding="0" cellspacing="0" style="border:solid #CCCCCC 1px"> <tr> <td height="25" colspan="6" align="center" bgcolor="#eeeeee"><strong><{$news.titles}></strong></td> </tr> <tr> <td width="74" height="25" align="right">作者:</td> <td width="220"><{$news.author}></td> <td width="40" align="right">时间:</td> <td width="134" align="left"><{$news.sj}></td> <td width="110" align="right">关键词:</td> <td width="220" align="left"><{$news.keyword}></td> </tr> <tr> <td height="25" align="right" >核心提示:</td> <td height="25" colspan="5" class="artical" style="border:double #FF0000 3px;"><{$news.sumary}></td> </tr> <tr> <td height="25" colspan="6" align="left" class="artical" style="padding:10px;"><{$news.contents}></td> </tr></table></body></html>
|