2013-03-05 54 views
0

我想通过smarty模板创建一个XML文件。为此,我将一个数组传递给模板文件。这是我用来生成数组并传递的代码。Smarty在tpl文件中不显示数组值

$correct_answers = explode(",", $answer['answer']); 
$smarty->assign('answers', $correct_answers); 

数组已成功生成,我通过使用print_r()进行了检查;但我的问题是,它在tpl文件中显示为空。如果我检查计数,它显示0.我无法获得数组值。这是模板文件代码。

{assign var = "inc" value="0"} 
{section name=answer loop=$answers} 
    <simpleChoice identifier="{$answers[answer]}">{$answers[answer]}</simpleChoice>   
    {assign var = "inc" value=$inc+1} 
{/section} 

我不知道我出错的地方。

阵列结构,

Array 
(
    [0] => Alonso 
    [1] => Jenson Button 
    [2] => Rubens Barrichello 
) 

回答

3

试试这个:与其section使用foreach

{foreach from=$answers item=answer} 
    <simpleChoice identifier="{$answer}">{$answer}</simpleChoice>   
    {assign var = "inc" value=$inc+1} 
{/foreach} 

这类似于在PHP foreach循环。

Ref:http://www.smarty.net/docsv2/en/language.function.foreach

+0

谢谢男人..它现在运作良好。 – 2013-03-05 06:28:24

+0

@EdwinAlex:酷:D – 2013-03-05 06:28:52