Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Brautkleider auf Topwedding.de

Find free files to download on allwhatyouwant.net

Looking For Affordable Wedding Dresses 2015 at Best Prices On TDBridal.com

Shop high quality cheap prom dresses on Dresswe.co.uk

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Advertisement

{foreach},{foreachelse}

{foreach}鐢ㄤ簬寰幆鏁扮粍銆 {foreach}鐨勮娉曟瘮{section}寰幆瑕佹洿绠鍗曞拰娓呮櫚锛屽苟涓斿彲浠ヤ娇鐢ㄩ潪鏁板瓧涓嬫爣鐨勬暟缁勩

{foreach $arrayvar as $itemvar}

{foreach $arrayvar as $keyvar=>$itemvar}

Note

foreach鐨勮娉曞彲浠ユ帴鍙楁病鏈夊悕绉扮殑灞炴э紝璇ヨ娉曟槸Smarty 3鏂板鐨勩傜劧鑰孲marty 2璇硶 {foreach from=$myarray key="mykey" item="myitem"}涔熷悓鏍锋敮鎸併

  • {foreach} 寰幆鍙互琚祵濂椾娇鐢.

  • array鍙橀噺锛屼竴鑸槸鏁扮粍鐨勫硷紝鍐冲畾浜{foreach} 寰幆鐨勬鏁般備綘涔熷彲浠ヤ紶閫掍竴涓换鎰忕殑鏁存暟鏉ユ帶鍒跺惊鐜鏁般

  • 濡傛灉array鏁扮粍鍙橀噺涓紝娌℃湁鍊肩殑鎯呭喌涓嬶紝 {foreachelse}灏嗘墽琛屻

  • {foreach}鐨勫睘鎬э細 @index, @iteration, @first, @last, @show, @total.

  • {foreach}鐨勮娉曞懡浠わ細 {break}, {continue}.

  • 浠f浛鎸囧畾key鍙橀噺锛屼綘鍙互閫氳繃 {$item@key}鏉ヤ娇鐢ㄥ惊鐜殑褰撳墠key銆(瑙佷笅闈㈢殑渚嬪瓙).

Note

$var@property鐨勮娉曟槸Smarty 3鏂板鐨勩傜劧鑰孲marty 2椋庢牸鐨勮娉{foreach from=$myarray key="mykey" item="myitem"}, $smarty.foreach.name.property涔熸槸鏀寔鐨勩

Note

鍗充娇浣犲湪寰幆璇硶閲{foreach $myArray as $myKey => $myValue} 宸茬粡鎸囧畾浜唊ey鐨勫彉閲忓悕锛屼絾寰幆浣撳唴$myValue@key杩樻槸鍙敤鐨勩

鍙夋爣璁:

鍚嶇О 璇存槑
nocache 鍏抽棴{foreach}寰幆鐨勭紦瀛

Example聽7.30.聽绠鍗曠殑{foreach} 寰幆


<?php
$arr = array('red', 'green', 'blue');
$smarty->assign('myColors', $arr);
?>

  

妯℃澘灏嗛『搴忚緭鍑$myColors


<ul>
{foreach $myColors as $color}
    <li>{$color}</li>
{/foreach}
</ul>

  

杈撳嚭锛


<ul>
    <li>red</li>
    <li>green</li>
    <li>blue</li>
</ul>

  

Example聽7.31.聽浣跨敤key鍙橀噺鐨勪緥瀛


<?php
$people = array('fname' => 'John', 'lname' => 'Doe', 'email' => 'j.doe@example.com');
$smarty->assign('myPeople', $people);
?>

  

妯℃澘灏嗕互閿煎鐨勬柟寮忚緭鍑$myArray


<ul>
{foreach $myPeople as $value}
   <li>{$value@key}: {$value}</li>
{/foreach}
</ul>

  

杈撳嚭锛


<ul>
    <li>fname: John</li>
    <li>lname: Doe</li>
    <li>email: j.doe@example.com</li>
</ul>

  

Example聽7.32.聽澶氱淮鏁扮粍閫氳繃itemkey鏉ュ祵濂椾娇鐢▄foreach}

澶氱淮鏁扮粍鐨勯敭涓鑸細瀵瑰簲鍙︿竴涓暟缁勩


<?php
 $smarty->assign('contacts', array(
                             array('phone' => '555-555-1234',
                                   'fax' => '555-555-5678',
                                   'cell' => '555-555-0357'),
                             array('phone' => '800-555-4444',
                                   'fax' => '800-555-3333',
                                   'cell' => '800-555-2222')
                             ));
?>

  

妯℃澘灏嗚緭鍑$contact.


{* key always available as a property *}
{foreach $contacts as $contact}
  {foreach $contact as $value}
    {$value@key}: {$value}
  {/foreach}
{/foreach}

{* accessing key the PHP syntax alternate *}
{foreach $contacts as $contact}
  {foreach $contact as $key => $value}
    {$key}: {$value}
  {/foreach}
{/foreach}

  

涓婇潰涓や釜渚嬪瓙閮戒細杈撳嚭锛


  phone: 555-555-1234
  fax: 555-555-5678
  cell: 555-555-0357
  phone: 800-555-4444
  fax: 800-555-3333
  cell: 800-555-2222

  

Example聽7.33.聽{foreachelse}鐨勬暟鎹簱渚嬪瓙

寰幆鏄剧ず鏁版嵁搴擄紙PDO锛夌粨鏋溿備緥瀛愭槸寰幆浜嗕竴涓狿HP鐨勮凯浠e櫒锛坕terator锛夎屼笉鏄竴涓暟缁勶紙array锛夈


<?php 
  include('Smarty.class.php'); 

  $smarty = new Smarty; 

  $dsn = 'mysql:host=localhost;dbname=test'; 
  $login = 'test'; 
  $passwd = 'test'; 

  // setting PDO to use buffered queries in mysql is 
  // important if you plan on using multiple result cursors 
  // in the template. 

  $db = new PDO($dsn, $login, $passwd, array( 
     PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)); 

  $res = $db->prepare("select * from users"); 
  $res->execute(); 
  $res->setFetchMode(PDO::FETCH_LAZY); 

  // assign to smarty 
  $smarty->assign('res',$res); 

  $smarty->display('index.tpl');?>
?>

  

{foreach $res as $r} 
  {$r.id} 
  {$r.name}
{foreachelse}
  .. no results .. 
{/foreach}

  

涓婇潰鐨勪緥瀛愭樉绀轰簡鏌ヨ缁撴灉idname涓や釜瀛楁鐨勫唴瀹广

杩唬鍣ㄥ拰鏁扮粍寰幆鍝釜鏇撮珮鏁堝憿锛熸暟缁勮岃█锛屾瘡娆″惊鐜箣鍓嶅叏閮ㄧ殑鏁扮粍鏁版嵁閮戒細琚厛鏀惧埌鍐呭瓨鍫嗘爤鍐咃紝鐒跺悗鍐嶈繘琛屽惊鐜 鑰岃凯浠e櫒姣忔杩唬寰幆鏃讹紝閮戒細杞藉叆骞堕噴鏀剧粨鏋滃唴瀹癸紝杩欐牱鍙互鑺傜渷杩愯鏃堕棿鍜屽唴瀛橈紝灏ゅ叾鏄綋缁撴灉闆嗛潪甯稿ぇ鐨勬椂鍊欍

@index

index鏄綋鍓嶆暟缁勭储寮曪紝浠0寮濮嬭绠椼

Example聽7.34.聽index 渚嬪瓙


{* output empty row on the 4th iteration (when index is 3) *}
<table>
{foreach $items as $i}
  {if $i@index eq 3}
     {* put empty table row *}
     <tr><td>nbsp;</td></tr>
  {/if}
  <tr><td>{$i.label}</td></tr>
{/foreach}
</table>

  

@iteration

iteration鏄綋鍓嶅惊鐜殑娆℃暟锛屽拰index涓嶅悓锛iteration鏄粠1寮濮嬨 iteration鍦ㄦ瘡娆″惊鐜殑鏃跺欓兘浼氬姞涓銆

Example聽7.35.聽iteration 渚嬪瓙: is div by

"is div by"杩愮畻鍙互瀵瑰惊鐜鏁板仛涓浜涚壒娈婄殑鍒ゆ柇銆 涓嬮潰鎴戜滑灏嗘瘡4娆″惊鐜氨杈撳嚭涓娆$矖浣撶殑鍚嶇О銆


{foreach $myNames as $name}
  {if $name@iteration is div by 4}
    <b>{$name}</b>
  {/if}
  {$name}
{/foreach}


Example聽7.36.聽iteration 渚嬪瓙: is even/odd by

"is even by""is odd by"鍙互鐢ㄤ簬鍦ㄥ惊鐜腑濂囧伓浜ゆ浛杩涜涓浜涙搷浣溿傚湪寮濮嬬殑鏃跺欏彲浠ラ夋嫨濂囨垨鍋剁殑寰幆銆 涓嬮潰鏄瘡涓夋寰幆浼氭敼鍙樹竴娆″瓧浣撻鑹层

 
 {foreach $myNames as $name}
   {if $name@iteration is even by 3}
     <span style="color: #000">{$name}</span>
   {else}
     <span style="color: #eee">{$name}</span>
   {/if}
 {/foreach}
 
 

杈撳嚭锛


    <span style="color: #000">...</span>
    <span style="color: #000">...</span>
    <span style="color: #000">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #000">...</span>
    <span style="color: #000">...</span>
    <span style="color: #000">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #eee">...</span>
    <span style="color: #eee">...</span>
    ...

   

@first

褰撳惊鐜{foreach}鏄娆″惊鐜椂锛first灏嗕负TRUE 涓嬮潰鎴戜滑鐢╢irst鏉ユ樉绀轰竴涓〃鏍肩殑琛ㄥご銆

Example聽7.37.聽first渚嬪瓙


{* show table header at first iteration *}
<table>
{foreach $items as $i}
  {if $i@first}
    <tr>
      <th>key</td>
      <th>name</td>
    </tr>
  {/if}
  <tr>
    <td>{$i@key}</td>
    <td>{$i.name}</td>
  </tr>
{/foreach}
</table>

  

@last

{foreach}寰幆鍒颁簡鏈鍚庝竴娆℃椂锛 last灏嗕负TRUE銆 涓嬮潰鎴戜滑灏嗗湪寰幆鐨勬渶鍚庢彃鍏ヤ竴鏉℃按骞崇嚎銆

Example聽7.38.聽last渚嬪瓙


{* Add horizontal rule at end of list *}
{foreach $items as $item}
  <a href="#{$item.id}">{$item.name}</a>{if $item@last}<hr>{else},{/if}
{foreachelse}
  ... no items to loop ...
{/foreach}

  

@show

show灞炴ф槸鍦{foreach}寰幆鎵ц涔嬪悗锛 妫娴嬪惊鐜槸鍚︽樉绀烘暟鎹殑鍒ゆ柇銆 show鏄竴涓竷灏斿笺

Example聽7.39.聽show渚嬪瓙


<ul>
{foreach $myArray as $name}
    <li>{$name}</li>
{/foreach}
</ul>
{if $name@show} do something here if the array contained data {/if}


@total

total鏄暣涓{foreach}寰幆鐨勬鏁般 total鍙互鍦{foreach}鍐呴儴锛屾垨鑰呬箣鍚庝娇鐢ㄣ

Example聽7.40.聽total渚嬪瓙


{* show number of rows at end *}
{foreach $items as $item}
  {$item.name}<hr/>
  {if $item@last}
    <div id="total">{$item@total} items</div>
  {/if}
{foreachelse}
 ... no items to loop ...
{/foreach}


鍙傝{section}, {for}{while}

{break}

{break}鍋滄寰幆銆

Example聽7.41.聽{break} 渚嬪瓙

 
  {$data = [1,2,3,4,5]}
  {foreach $data as $value}
    {if $value == 3}
      {* abort iterating the array *}
      {break}
    {/if}
    {$value}
  {/foreach}
  {*
    prints: 1 2
  *}
 
   

{continue}

{continue}灏嗚烦杩囧綋鍓嶆湰娆″惊鐜苟杩涘叆涓嬩竴娆″惊鐜

Example聽7.42.聽{continue} 渚嬪瓙

 
  {$data = [1,2,3,4,5]}
  {foreach $data as $value}
    {if $value == 3}
      {* skip this iteration *}
      {continue}
    {/if}
    {$value}
  {/foreach}
  {*
    prints: 1 2 4 5
  *}
 
   

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 2 plus 18? (Are you human?)

Advertisement