简体   繁体   English

Smarty在tpl文件中不显示数组值

[英]Smarty not displaying array values in tpl file

I want to create an XML file through smarty template. 我想通过smarty模板创建XML文件。 For this, i am passing an array to the template file. 为此,我将数组传递给模板文件。 This is the code i am using to generate array and to pass. 这是我用来生成数组并传递的代码。

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

The array is generated successfully and i checked it by using print_r() ; 该数组已成功生成,我通过使用print_r()进行了检查; But my problem is, it shows empty in the tpl file. 但是我的问题是,它在tpl文件中显示为空。 If i check the count, it shows 0. I cannot get the array values. 如果我检查计数,它将显示0。我无法获取数组值。 This is the template file code. 这是模板文件代码。

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

I don't know where i went wrong. 我不知道我哪里做错了。

Array Structure is, 数组结构是

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

Try this : Instead of section use foreach . 试试这个:代替section使用foreach

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

This is similar to foreach loop in php. 这类似于php中的foreach循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM