本文實例總結了smarty中常用方法。分享給大家供大家參考。具體如下:
1. Smarty中foreach的index、iteration的使用
.index包含當前數組索引,從零開始。
index示例
{* The header block is output every five rows *}
{* 每五行輸出一次頭部區塊 *}
<table>
{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo.index % 5 == 0}
<tr><th>Title</th></tr>
{/if}
<tr><td>{$i.label}</td></tr>
{/foreach}
</table>
.iteration包含當前循環次數,與index不同,從1開始,每次循環增長1。
iteration和index示例
{* this will output 0|1, 1|2, 2|3, ... etc *}
{* 該例將輸出0|1, 1|2, 2|3, ... 等等 *}
{foreach from=$myArray item=i name=foo}
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
{/foreach}
2. smarty中section的嵌套使用
示例1:
$bookmarks = array('0' => array('name'=> 'n1', 'url'=>'url2'), '1' => array('name'=> 'n21', 'url'=>'url22'));
$categories= array('0' => array('cate_id'=> 'n1', 'cate_name'=>'url2'), '1' => array('cate_id'=> 'n21', 'cate_name'=>'url22'));
{section name='bm' loop=$bookmarks}
Name:$bookmarks[bm].name
URL:$bookmarks[bm].url
{section name='cate' loop=$categories[bm]}
$categories[bm][cate].cate_id
$categories[bm][cate].cate_name
{/section}
{/section}
::::
示例2:
$lists = array();
for(...){
$oneList['dateTime'] = date("Y-m-d");
$oneList['detailList'] = array();
for(....){
$oneList['detailList'][$j]['count'] = $onecout;
$oneList['detailList'][$j]['title'] = $onetitle;
}
$lists[] = $oneList;
}
:::::
{section name=loop loop=$lists}
{$lists[loop].dateTime}
{section name=loop2 loop=$lists[loop]["detailList"]}
{$lists[loop]['detailList'][loop2].title}
{$lists[loop]["detailList"][loop2].count}
{/section}
{/section}
3. 其他常用關鍵字
<{section loop= $varName[,start=$start,step=$setp,max=$max,$show=true]}>
name: section的名稱,不用加$;
$loop: 要循環的變量,程度中要使用assign對這個變量進行操作。
$start: 開始循環的下標。默認為0;
$step: 每次循環下標的增數;
$show : boolean型。決定是否對於這塊進行顯示。默認為true;
<{section}>的屬性;
index:循環下標。默認為0;
index_prev:當前下標的上一個值,默認為-1;
index_next:當前下標的下一個值,默認為1;
first:是否為第一下循環;
last:是否為最後一個循環;
iteration:循環個數;
rownum:當前行號,iteration的別名;
loop:最後一個循環號。Section的循環次數;
show:是否顯示;
<{section loop=$News}>
新聞編號:<{$News[loop].newID}><br>
新聞內容:<{$News[loop].newTitle}><br>
<{sectionelse}>
I am sorry
<{/section}>
if用法:
{if $list[row].name eq "1"}
星期1
{elseif $list[row].name=="2"}
星期2
{else}
默認
{/if}
4. smarty 系統變量
{* 顯示URL中的page值($_GET)http://www.example.com/index.php?page=foo *}
{$smarty.get.page}
{* 顯示來自一個表單的"page"變量($_POST['page'])*}
{$smarty.post.page}
{* 顯示COOKIE變量"username"的值($_COOKIE['username'])*}
{$smarty.cookies.username}
{* 顯示服務器變量"SERVER_NAME"($_SERVER['SERVER_NAME'])*}
{$smarty.server.SERVER_NAME}
{$smarty.server.PHP_SELF}
{$smarty.server.SCRIPT_NAME}
{* 顯示系統環境變量"PATH" *}
{$smarty.env.PATH}
{* 顯示PHP會話變量"id"($_SESSION['id'])*}
{$smarty.session.id}
{* 顯示變量"username",不論來自get/post/cookies/server/env *}
{$smarty.request.username}
$smarty}保留變量可以被用於訪問一些特殊的模板變量,以下是全部頁面請求變量。
以下是訪問頁面請求變量諸如get,post,cookies,server,enviroment和session變量的例子. 例如{$smarty.server.SERVER_NAME}取得服務器變量,{$smarty.env.PATH}取得系統環境變量path,{$smarty.request.username}取得get/post/cookies/server/env的復合變量。
{$smarty.now}變量用於訪問當前時間戳.
可以用 date_format調節器格式化輸出. 例如{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
{$smarty.const}
你可以直接訪問PHP常量. 例如{$smarty.const._MY_CONST_VAL}
{$smarty.capture}
可以通過{capture}..{/capture}結構 截取的輸出可以使用{$smarty} 變量訪問.
{$smarty.config}
{$smarty}變量 可以訪問已經加載的config變量.
例如 {$smarty.config.foo}就可以表示 {#foo#}.
{$smarty.section}, {$smarty.foreach}
{$smarty} 變量可以訪問'section'和'foreach'循環的屬性.
{$smarty.template}
顯示當前被處理的模板的名字.
{$smarty.version}
顯示smarty模板的版本
{$smarty.ldelim}
顯示左分隔符
{$smarty.rdelim}
顯示右分隔符$smarty}保留變量可以被用於訪問一些特殊的模板變量,以下是全部頁面請求變量。
以下是訪問頁面請求變量諸如get,post,cookies,server,enviroment和session變量的例子. 例如{$smarty.server.SERVER_NAME}取得服務器變量,{$smarty.env.PATH}取得系統環境變量path,{$smarty.request.username}取得get/post/cookies/server/env的復合變量。
{$smarty.now}變量用於訪問當前時間戳.
可以用 date_format調節器格式化輸出. 例如{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
{$smarty.const}
你可以直接訪問PHP常量. 例如{$smarty.const._MY_CONST_VAL}
{$smarty.capture}
可以通過{capture}..{/capture}結構 截取的輸出可以使用{$smarty} 變量訪問.
{$smarty.config}
{$smarty}變量 可以訪問已經加載的config變量.
例如 {$smarty.config.foo}就可以表示 {#foo#}.
{$smarty.section}, {$smarty.foreach}
{$smarty} 變量可以訪問'section'和'foreach'循環的屬性.
{$smarty.template}
顯示當前被處理的模板的名字.
{$smarty.version}
顯示smarty模板的版本
{$smarty.ldelim}
顯示左分隔符
{$smarty.rdelim}
顯示右分隔符
希望本文所述對大家基於smarty模板的php程序設計有所幫助。