2013-06-21 71 views
0

以下是在book如何以下.tpl文件的工作,以顯示錶

{* products_list.tpl *} 
    {load_presentation_object filename="products_list" assign="obj"} 

     {if $obj->mProducts} 
     <table class="product-list" border="0"> 
     <tbody> 
     {section name=k loop=$obj->mProducts} 
     {if $smarty.section.k.index % 2 == 0} 
     <tr> 
     {/if} 
    <td valign="top"> 
    <h3 class="product-title"> 
    <a href="{$obj->mProducts[k].link_to_product}"> 
    {$obj->mProducts[k].name} 
    </a> 
    </h3> 
    <p> 
    {if $obj->mProducts[k].thumbnail neq ""} 
    <a href="{$obj->mProducts[k].link_to_product}"> 
    <img src="{$obj->mProducts[k].thumbnail}" 
    alt="{$obj->mProducts[k].name}" /> 
    </a> 
    {/if} 
    {$obj->mProducts[k].description} 
    </p> 

    <p class="section"> 
    Price: 
    {if $obj->mProducts[k].discounted_price != 0} 
    <span class="old-price">{$obj->mProducts[k].price}</span> 
    <span class="price">{$obj->mProducts[k].discounted_price}</span> 
    {else} 
    <span class="price">{$obj->mProducts[k].price}</span> 
    {/if} 
    </p> 
    </td> 
    {if $smarty.section.k.index % 2 != 0 && !$smarty.section.k.first || 
    $smarty.section.k.last} 
    </tr> 
    {/if} 
    {/section} 
    </tbody> 
    </table> 
    {/if} 

給出一個代碼我不清楚有以下行

{if $smarty.section.k.index % 2 == 0} 

爲什麼上面的語句寫的,看看指數是偶數還是奇數,這對桌面顯示有什麼影響?

回答

2

它每兩個數組項創建一個新的錶行。

//open 
{if $smarty.section.k.index % 2 == 0} 
    <tr> 
{/if} 

//close 
{if $smarty.section.k.index % 2 != 0 && !$smarty.section.k.first || 
    $smarty.section.k.last} 
    </tr> 
{/if} 

這個例子看看:!

echo "<table border='1'>\n"; 

for($i = 0; $i<=10; $i++) { 

    if ($i % 2 == 0) { 
     echo "<tr>\n"; 
    } 

    echo "<td> ". $i. "contents </td> \n"; 

    if ($i % 2 != 0) { 
     echo "</tr> \n"; 
    } 

} 

echo "<table />\n"; 
+0

那麼到底什麼'{如果$ smarty.section.k.index%2 = 0 && $ smarty.section.k.first | | $ smarty.section.k.last}'行在那裏結束{/ tr} –

+0

更新的答案,應該更清楚。 –

+0

我得到下面的輸出一個水平線'0contents1contents2contents3contents4contents5contents6contents7contents8contents9conconnts10contents' –