2011-05-29 64 views
1

我试图用smarty模板引擎在数组中打印子数组。说我有这样的阵列结构:在smarty中打印数组中的子数组(foreach)

Array 
    (
[0] => Array 
    (
     [directory] => One Day As A Lion - 2011-03-01/ 
     [source_notes] => iRiver 
     [recording_type] => Audience 
     [type] => Audio 
     [source_type] => MP3 
     [fileList] => Array 
      (
       [0] => 09 One Day As A Lion.mp3 
       [1] => 01 Intro.mp3 
       [2] => 05 Ocean View.mp3 
       [3] => 04 Swashbuckler.mp3 
       [4] => One Day As A Lion - 2011-03-01 - Prince Bandroom, Melbourne, Australia.jpg 
       [5] => 10 Peart Plus.mp3 
       [6] => 06 Rockers.mp3 
       [7] => 03 Last Letter.mp3 
       [8] => 07 Swampy.mp3 
       [9] => 02 If You Fear Dying.mp3 
       [10] => 08 Wild International.mp3 
      ) 

    ) 

) 

我到底该如何得到含smarty打印文件名的数组?目前我有一个smarty foreach循环,看起来像:

{foreach $sources as $sourceInfo} 
    {strip}  
    Recording Type: {$sourceInfo.type} : {$sourceInfo.recording_type}<br> 
    Source : {$sourceInfo.source_notes}<br>  
    {/strip} 
    {/foreach} 

而且我不知道如何实现第二个foreach循环。有没有人有什么建议?我对文档有点困惑,因为似乎有两种嵌套的foreach循环方法,其中一种似乎不推荐使用。是否foreach循环是最好的方式呢,还是在smarty中有另一种推荐的方法?任何反馈将不胜感激。谢谢!

回答

4

只需添加其他的foreach:

{foreach from=$sources item=sourceInfo} 
    {strip}  
    Recording Type: {$sourceInfo.type} : {$sourceInfo.recording_type}<br> 
    Source : {$sourceInfo.source_notes}<br>  
    Files: {foreach from=$sourceInfo.fileList item=file}{$file}, {foreachelse}<i>no files</i>{/foreach} 
    {/strip} 
{/foreach} 
+0

谢谢!我觉得这很简单。我不知道你可以像smarty那样访问子数组。尽管你必须改变上面的一行,所以它的“Files:{foreach $ sourceInfo.fileList as $ file} {$ file}
{foreachelse} 没有文件 {/ foreach}”< - 基本上你需要括号$文件。 – thomascirca 2011-05-29 18:31:46

+0

啊,是的,错过了。你当然是对的。 – Czechnology 2011-05-29 18:52:34