1,smarty模版中if的用法
<?php $root=str_replace("\\","/",dirname(__FILE__)); define("ROOT",$root."/"); define("DOMAIN","http://localhost/testSmarty"); require(ROOT."smarty/Smarty.class.php"); $smarty=new Smarty(); $smarty->template_dir=ROOT."demo/templates/"; $smarty->compile_dir=ROOT."demo/templates_c/"; $smarty->use_sub_dirs=false; $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; $smarty->caching="0"; $smarty->cache_dir=ROOT."demo/caches/"; $smarty->cache_lifetime=3*24*(60*60);//0 $smarty->assign("num",33); $smarty->display("testif.html"); ?>
模版文件:testif.html
<{if $num eq 22}>
這部分執行了22
<{elseif $num eq 33}>
這部分執行了33
<{else}>
沒有輸出任何東西
<{/if}>
2,if的其他知識
Smarty 中的 if 語句和 php 中的 if 語句一樣靈活易用,並增加了幾個特性以適宜模板引擎. if 必須於 /if 成對出現. 可以使用 else 和 elseif 子句. 可以使用以下條件修飾詞:eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、>、<、<=、>=. 使用這些修飾詞時必須和變量或常量用空格格開.
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 $_global_password} 是判斷變量存不存在....
{if $_global_password!==''} 是判斷變量等不等於空....
equal : 相等、not equal:不等於、greater than:大於、less than:小於、less than or equal:小於等於、great than or equal:大於等於、is even:是偶數、is odd:是奇數、is not even:不是偶數、is not odd:不是奇數、not:非、mod:取余、div by:被。。。除
