2012-10-19 25 views
0

我的情景:Smarty {foreachelse}有什麼用?

<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Email</th> 
     </tr> 
    </thead> 
    <tbody> 
     {foreach from=$users item=user} 
      <tr> 
       <td>{$user.name}</td> 
       <td>{$user.email}</td> 
      </tr> 
     {foreachelse} 
      There are no users.... 
     {/foreach} 
    </tbody> 
</table> 

現在,當沒有用戶我有一個醜陋的表,所以我會補充:

{if $users|count > 0} 
    <table> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>Email</th> 
      </tr> 
     </thead> 
     <tbody> 
      {foreach from=$users item=user} 
       <tr> 
        <td>{$user.name}</td> 
        <td>{$user.email}</td> 
       </tr> 
      {foreachelse} 
       There are no users.... 
      {/foreach} 
     </tbody> 
    </table> 
{else} 
    There are no users.... 
{/if} 

但現在我的{foreachelse}是沒用的。

因此,我刪除{foreachelse} There are no users....部分,並得出結論{foreachelse}是無用的。

這個問題我已經在<table><ol><ul>等。

有誰有解決的辦法,所以我能夠使用{foreachelse}

謝謝!

+0

不知道我明白。爲什麼該方法僅僅因爲您可以使用其他方法而無用? –

+0

不,因爲我僅使用'{foreach}'來創建表或列表。在這種情況下,{foreachelse}無用(或者有人知道更好的解決方案)。 –

+3

當你想/需要輸出一行表示沒有行時,foreachelse很有用。非常方便的結構,你必須有一張桌子,不管是什麼。 – BoltClock

回答

2

你可以有一排它說沒有用戶,如:

<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Email</th> 
     </tr> 
    </thead> 
    <tbody> 
     {foreach from=$users item=user} 
      <tr> 
       <td>{$user.name}</td> 
       <td>{$user.email}</td> 
      </tr> 
     {foreachelse} 
      <tr> 
       <td colspan="2">There are no users....</td> 
      </tr> 
     {/foreach} 
    </tbody> 
</table> 

,但我能理解它時,這是不理想的結果。

有時您可能會在foreach中使用div而不是表格或列出內容。檢查smarty文檔: http://www.smarty.net/docs/en/language.function.foreach.tpl

+0

你是對的,而不是理想的結果。 –

1

{foreachelse}from變量中沒有值時執行。這對我和if-else條件是不需要的。如果我使用您的代碼,那麼我會把{foreachelse}這樣,

{foreachelse} 
    <tr><td colspan="2" style="color:red">There are no users....</td></tr> 
{/foreach} 
+0

不是預期的結果。表中的反饋與標題是真實的醜陋和混亂。 –

+0

那麼,在你的情況下,你不必使用'{foreachelse}'部分。 –

+0

那麼告訴我,你什麼時候使用'{foreachelse}'? –