[英]Smarty not displaying array values in tpl file
我想通過smarty模板創建XML文件。 為此,我將數組傳遞給模板文件。 這是我用來生成數組並傳遞的代碼。
$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
)
試試這個:代替section
使用foreach
。
{foreach from=$answers item=answer}
<simpleChoice identifier="{$answer}">{$answer}</simpleChoice>
{assign var = "inc" value=$inc+1}
{/foreach}
這類似於php中的foreach
循環。
參考: http : //www.smarty.net/docsv2/zh/language.function.foreach
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.