{"id":284,"date":"2011-06-05T11:58:34","date_gmt":"2011-06-05T03:58:34","guid":{"rendered":"http:\/\/www.youthtribe.com\/?p=284"},"modified":"2011-06-05T11:58:34","modified_gmt":"2011-06-05T03:58:34","slug":"smarty-if-else-if%e5%88%a4%e6%96%b7-%e6%95%b0%e6%8d%ae%e6%8d%a2%e8%a1%8c%e6%98%be%e7%a4%ba","status":"publish","type":"post","link":"http:\/\/www.youthtribe.com\/archives\/284","title":{"rendered":"Smarty if else \/if\u5224\u65b7 \u6570\u636e\u6362\u884c\u663e\u793a"},"content":{"rendered":"

Smarty if else \/if\u5224\u65b7<\/h4>\n
\n
\n
if\u8bed\u53e5\u548c\u548c\u6761\u4ef6\u540cphp\u5dee\u4e0d\u591a\uff0c\r\n\u4f46\u6bcf\u4e2a\u8bcd\u4e4b\u95f4\u5fc5\u987b\u7528\u7a7a\u683c\u5206\u5272\u5f00\u3002\r\n\u4e5f\u6709\u4e00\u4e9b\u65b0\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\r\n\u5217\u4e3e\u5982\u4e0b\uff1a\r\n eq\u76f8\u7b49\uff0c\r\n ne\u3001neq\u4e0d\u76f8\u7b49\uff0c\r\n gt\u5927\u4e8e\uff0c\r\n lt\u5c0f\u4e8e\uff0c\r\n gte\u3001ge\u5927\u4e8e\u7b49\u4e8e\uff0c\r\n lte\u3001le \u5c0f\u4e8e\u7b49\u4e8e\uff0c\r\n not\u975e\uff0c mod\u6c42\u6a21\u3002 \r\nis [not] div by\u662f\u5426\u80fd\u88ab\u67d0\u6570\u6574\u9664\uff0c\r\n is [not] even\u662f\u5426\u4e3a\u5076\u6570\uff0c\r\n $a is [not] even by $b\u5373($a \/ $b) % 2 == 0\uff0c\r\n is [not] odd\u662f\u5426\u4e3a\u5947\uff0c\r\n $a is not odd by $b\u5373($a \/ $b) % 2 != 0 \u793a\u4f8b\uff1a \r\n\r\n{if $name eq \"Peter\"} \r\nWelcome male. \r\n{elseif $name eq \"Fengfeng\"} \r\nWelcome female. \r\n{else} Welcome, whatever you are. {\/if}<\/pre>\n
\u6362\u884c<\/pre>\n
{*section name=loop loop=$daili*}\r\n\r\n<td height=\"24\" align=\"center\"><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>\r\n{*if $smarty.section.loop.rownum is div by 4*}<\/tr><tr><\/tr>{*\/if*}  \/\/\u8fd9\u884c\u4ee3\u7406\u662f\u6362\u884c\u7684\u610f\u601d\r\n{*\/section*}\r\n\r\n\r\n<!--{foreach from=$pd1 item=p key=i name=x}--> \r\n<td>\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> \r\n<tr><td>\r\n<a href="\""<!--{$p.ProductPhoto2}-->\" onclick=\"return hs.expand(this)\">\r\n<img width=\"230\" height=\"150\" border=\"0\" title=\"<!--{$Html.ClickEnlarge}-->\" src=\"<!--{$p.ProductPhoto1}-->\" \/>\r\n<\/a>\r\n<\/td><\/tr>\r\n<tr><td align=\"center\" height=\"25\">\r\n<!--{* \u622a\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u540d\u79f0\u5e76\u5c06\u641c\u7d22\u5b57\u66ff\u6362\u6210\u7ea2\u8272 *}--> \r\n<a target=\"_blank\" href="\""?File=product_detail&Lang=<!--{$Sys.Lang}-->&ID=<!--{$p.ID}-->\">\r\n<!--{if $p.ProductNameColor == \"\"}--> \r\n\u4ea7\u54c1\u540d\u79f0\uff1a<!--{$p.ProductName|SongTruncate:6|replace:$smarty.get.wd:$pt.Redwd}--> \r\n<!--{else}--> \r\n<font color=\"<!--{$p.ProductNameColor}-->\"><!--{$p.ProductName|SongTruncate:6|replace:$smarty.get.wd:$pt.Redwd}--><\/font>\r\n<!--{\/if}--> \r\n<\/a>\r\n<\/td><\/tr>\r\n<tr><td align=\"center\" height=\"25\">\u4ea7\u54c1\u578b\u53f7\uff1a<!--{$p.ProductModel}--><\/td><\/tr>\r\n<tr><td>&nbsp;<\/td><\/tr>\r\n<\/table>\r\n<\/td>\r\n<!--{if $smarty.foreach.x.iteration is div by 2}--><\/tr><tr><!--{\/if}--> \r\n<!--{foreachelse}-->\r\n<td height=\"100\"><!--{$Html.NotProduct}--><\/td><!--{* \u6ca1\u6709\u6570\u636e\u65f6\u663e\u793a *}-->\u00a0\u00a0\u00a0     \r\n<!--{\/foreach}-->\u00a0\u00a0<\/pre>\n
<\/pre>\n
Smarty\u4e2d\u6279\u91cf\u751f\u6210html\u7684\u65b9\u6cd5<\/pre>\n
\u7ecf\u8fc7\u4e00\u756a\u6478\u7d22\uff0c\u7ec8\u4e8e\u627e\u5230\u65b9\u6cd5\uff0c\u5199\u51fa\u6765\u5927\u5bb6\u6307\u70b9\u4e00\u4e0b\uff1a\r\n\u9996\u5148\u5728smartt.class.php\u4e2d\u4e3aSmarty\u7c7b\u6dfb\u52a0\u8fd9\u4e48\u4e00\u4e2a\u65b9\u6cd5\uff1a\r\nPHP\u4ee3\u7801:\r\n\/\/\u53c2\u6570\u4e00\uff1ahtml\u6587\u4ef6\u4fdd\u5b58\u8def\u5f84\uff0c\u53c2\u65702\uff1a\u5199\u5165\u7684\u5185\u5bb9<\/pre>\n
\r\n\u4ee5\u4e0b\u4e3a\u5f15\u7528\u7684\u5185\u5bb9\uff1a\r\nfunction MakeHtmlFile($file_name, $c) \r\n{  \r\n\u00a0\u00a0\u00a0\u00a0\u00a0    if(!$fp = fopen($file_name, \"wa\"))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0    {\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    echo \"\u6587\u4ef6\u6253\u5f00\u5931\u8d25\uff01\";\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    return false;\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0\u00a0\u00a0\u00a0    }\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0\u00a0\u00a0\u00a0    if(!fwrite($fp, $c))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0    {\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    echo \"\u6587\u4ef6\u5199\u5165\u5931\u8d25\uff01\";\u00a0\u00a0\u00a0\u00a0<\/pre>\n
\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    fclose($fp);\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    return false;\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0\u00a0\u00a0\u00a0    }\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0\u00a0\u00a0\u00a0    fclose($fp);\u00a0\u00a0\u00a0\u00a0    \r\n\u00a0\u00a0    }<\/pre>\n
<\/pre>\n
<\/pre>\n
\r\n\u95ee\u9898\uff1a\u6211\u7684\u6240\u6709\u6587\u7ae0\u90fd\u8c03\u7528\u7684\u662fnews.tpl\u8fd9\u4e2a\u6a21\u677f\uff0c\u90a3\u4e48\u7a76\u7adf\u5982\u4f55\u6279\u91cf\u751f\u6210\u5462\uff1f<\/pre>\n
\u6211\u4eec\u5148\u770b\u770bnews.php\r\nPHP\u4ee3\u7801:<\/pre>\n
\r\n\u4ee5\u4e0b\u4e3a\u5f15\u7528\u7684\u5185\u5bb9\uff1a\r\n<?php\r\ninclude_once(\"config.php\");\r\ninclude_once(\"init.php\");\r\n$s->assign(\"title\",\"\u6240\u6709\u7684\u65b0\u95fb\u5206\u7c7b\");\r\n$ID=$_GET[\"ID\"]+0;\r\n$sql=\"select * from artical where newsID=$ID\";\r\n$rs=$db->fetch($sql);\r\n$s->assign(\"news\",$rs[\"rec\"][0]);\/\/\u6ce8\u610f\uff1a$rs[\"rec\"][0]\u662f\u4e2a\u6570\u7ec4\r\n$s->display(\"news.html\");\r\n?><\/pre>\n
<\/pre>\n
\r\n\u6574\u4e2a\u6a21\u677f\u53d8\u91cf\u5c31\u53ea\u6709$news,\u8fd9\u4e48\u5199\u6709\u4ec0\u4e48\u597d\u5904\u5462\uff1f\u6211\u53ef\u4ee5\u4ee5\u6570\u7ec4\u7684\u65b9\u5f0f\u8bfb\u53d6\u5185\u5bb9<\/pre>\n
\u90a3\u4e48\u7a76\u7adf\u5982\u4f55\u751f\u6210\uff1a\r\n\u770b\u5982\u4e0b\u4ee3\u7801\uff1a\r\n\u5f88\u7b80\u5355\u7684\r\nPHP\u4ee3\u7801:<\/pre>\n
\r\n\u4ee5\u4e0b\u4e3a\u5f15\u7528\u7684\u5185\u5bb9\uff1a\r\n<?php\r\ninclude_once(\"config.php\");\r\ninclude_once(\"init.php\");\r\n$sql=\"select * from artical\";\r\n$rs=$db->fetch($sql);\r\nforeach ($rs[\"rec\"] as $k=>$v)\r\n{\r\n$s->assign(\"news\",$v);\r\n$s->MakeHtmlFile(\".\/news\/news_\".$v[0].\".html\",$s->fetch(\"news.html\",null, null, false));\r\n}\r\n?><\/pre>\n
<\/pre>\n
\u5f88\u7b80\u5355\u628a\uff01<\/pre>\n
<\/pre>\n
\r\nPHP\u4ee3\u7801:<\/pre>\n
\u4ee5\u4e0b\u4e3a\u5f15\u7528\u7684\u5185\u5bb9\uff1a\r\n<!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]\">\r\n<html xmlns=\"[url=http:\/\/www.w3.org\/1999\/xhtml]http:\/\/www.w3.org\/1999\/xhtml[\/url]\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\r\n<title><{$news.titles}><\/title>\r\n<link href="\""<{$docroot}>\/CSS\/main.css\" rel=\"stylesheet\" type=\"text\/css\" \/>\r\n<\/head><\/pre>\n
<body>\r\n<table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"border:solid #CCCCCC 1px\">\r\n  <tr>\r\n\u00a0\u00a0\u00a0    <td height=\"25\" colspan=\"6\" align=\"center\" bgcolor=\"#eeeeee\"><strong><{$news.titles}><\/strong><\/td>\r\n  <\/tr>\r\n  <tr>\r\n\u00a0\u00a0\u00a0    <td width=\"74\" height=\"25\" align=\"right\">\u4f5c\u8005\uff1a<\/td>\r\n\u00a0\u00a0\u00a0    <td width=\"220\"><{$news.author}><\/td>\r\n\u00a0\u00a0\u00a0    <td width=\"40\" align=\"right\">\u65f6\u95f4\uff1a<\/td>\r\n\u00a0\u00a0\u00a0    <td width=\"134\" align=\"left\"><{$news.sj}><\/td>\r\n\u00a0\u00a0\u00a0    <td width=\"110\" align=\"right\">\u5173\u952e\u8bcd\uff1a<\/td>\r\n\u00a0\u00a0\u00a0    <td width=\"220\" align=\"left\"><{$news.keyword}><\/td> \r\n  <\/tr>\r\n  <tr>\r\n\u00a0\u00a0\u00a0    <td height=\"25\" align=\"right\" >\u6838\u5fc3\u63d0\u793a\uff1a<\/td>\r\n\u00a0\u00a0\u00a0    <td height=\"25\" colspan=\"5\" style=\"border:double #FF0000 3px;\"><{$news.sumary}><\/td>\r\n  <\/tr>\r\n  <tr>\r\n\u00a0\u00a0\u00a0    <td height=\"25\" colspan=\"6\" align=\"left\" style=\"padding:10px;\"><{$news.contents}><\/td>\r\n  <\/tr>\r\n<\/table>\r\n<\/body>\r\n<\/html><\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"

Smarty if else \/if\u5224\u65b7 if\u8bed\u53e5\u548c\u548c\u6761\u4ef6\u540cphp\u5dee\u4e0d\u591a\uff0c \u4f46\u6bcf\u4e2a\u8bcd\u4e4b\u95f4\u5fc5\u987b\u7528\u7a7a\u683c\u5206\u5272\u5f00\u3002 \u4e5f\u6709\u4e00\u4e9b\u65b0\u7684\u6761\u4ef6\u8bed\u53e5\uff0c \u5217\u4e3e\u5982\u4e0b\uff1a eq\u76f8\u7b49\uff0c ne\u3001neq\u4e0d\u76f8\u7b49\uff0c gt\u5927\u4e8e<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,9],"tags":[],"_links":{"self":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/284"}],"collection":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":1,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":285,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/284\/revisions\/285"}],"wp:attachment":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}