聪明的tpl foreach

时间:2012-07-04 16:34:34

标签: php foreach smarty

我使用smarty tpl。

现在我有一个PHP代码,我想在tpl中使用它:

Tis是数组的输出。

 stdClass Object
(
[Status] => success
[Time] => 2011-01-01 12:00:00
[IP] => 12.34.56.789
[Count] => 1
[Filter] => stdClass Object
    (
        [Sort] => ProductCode
        [Order] => DESC
        [Search] => 
        [ProductCode] => P001
        [ProductType] => 
    )

[Result] => stdClass Object
    (
        [Products] => Array
            (
                [0] => stdClass Object
                    (
                        [Identifier] => 1
                        [ProductCode] => test
                        [ProductName] => test
                    )

            )

    )

Tis是有效的PHP代码:

   foreach ($product_list->Result->Products as $product) {
        echo $product->ProductName;
}

我使用了跟随tpl代码,但这不起作用

           //in php
    $smarty->assign("data", $product_list); 

     //in tpl file
    {foreach item=item from=$data}

<tr>
    <td align="center">{$item.Result->Products->ProductName}</td>
    <td align="center"></td>
    <td align="center"></td>
    <td align="center"><a href="" class="usuallink">{$header.order}</a></td>
</tr>
{/foreach}

现在有人如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

来自php的工作代码如下所示:

{foreach from=$data->Result->Products item=item}
    {$item->ProductName}
{/foreach}

在smarty3中,它看起来更像是纯粹的PHP:

{foreach $data->Result->Products as $item}
    {$item->ProductName}
{/foreach}