smarty中常用方法執行個體總結_php執行個體

來源:互聯網
上載者:User
本文執行個體總結了smarty中常用方法。分享給大家供大家參考。具體如下:

1. Smarty中foreach的index、iteration的使用

.index包含當前數組索引,從零開始。

index樣本

{* The header block is output every five rows *}{* 每五行輸出一次頭部區塊 *}
 
  {foreach from=$items key=myId item=i name=foo}  {if $smarty.foreach.foo.index % 5 == 0}   
   {/if} 
   {/foreach}
  
Title
{$i.label}

.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}>
新聞內容:<{$News[loop].newTitle}>
<{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程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.