0

I want to create an XML file through smarty template. 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(); But my problem is, it shows empty in the tpl file. If i check the count, it shows 0. I cannot get the array values. 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
)

1 Answer 1

3

Try this : Instead of section use 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.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.