为什么在smarty foreach循环中出现以下错误?

时间:2013-11-28 06:12:34

标签: foreach smarty

以下是我在smarty模板中的foreach循环代码:

{if $question_detail.question_has_sub_ques=='yes' && $question_detail.sub_question}
      {foreach from=$question_detail.sub_question item=sub_ques_ans key=sub_ques_no}
      <tr class="oddRow">
        <td class="question">
          <h4><strong>Sub-question {$page_question_no+1}.{$sub_ques_no+1}</strong></h4>
          <ul class="options">
            <li><span>{$sub_ques_ans.question_text}</span></li>
          </ul>
        </td>
        <td class="question-id">QUE{$sub_ques_ans.question_id}</td>
      </tr>
        {if $sub_ques_ans.question_file!=''}
      <tr>
        <td valign="top" colspan="2">
          <img src="{$ques_thum_image_path}{$sub_ques_ans.question_id}_{$sub_ques_ans.question_file}">
        </td>
      </tr>
        {/if}
        {if $question_detail.sub_question_answer_{$page_question_no+1}}
      <tr class="question_answer evenRow">
        <td class="question">
          <p class="answers"><strong>Answers</strong></p> 
          <ol class="options">
          **{foreach from=$question_detail.sub_question_answer_{$page_question_no+1} item=sub_ans key=sub_ans_no}**
            <li>
            {if $sub_ans.answer_is_right==1} {assign var='correct_sub_ans' value=$sub_ans_no+1} {/if}
            {if $sub_ans.answer_text!=''}{$sub_ans.answer_text}{/if}<br />
            {if $sub_ans.answer_file!=''}<img src="{$ans_thumb_img_path}{$sub_ans.answer_id}_{$sub_ans.answer_file}" >{/if}
            </li>  
          {/foreach}
          </ol>
          <p class="correct-answer"><strong>Correct Answer Option : </strong>{$correct_sub_ans}</p>
        </td>
        <td class="question-id">&nbsp;</td>
      </tr>
        {/if}  
      {/foreach}
    {/if}

我收到的错误如下:

  

致命错误:Smarty错误:[在question-details.tpl第66行]:

     

语法错误:foreach:在第1095行的/var/www/eprime/ocn/core/libs/Smarty.php中缺少'item'属性(SmartyCompiler.php,第1179行)

我的代码中的第66行是:

{foreach from=$question_detail.sub_question_answer_{$page_question_no+1} item=sub_ans key=sub_ans_no}

有人可以帮我纠正这个错误吗?任何形式的帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

试试这个

{assign var="condition" value="{$question_detail.sub_question_answer}_{$page_question_no+1}"}
{foreach from=$condition item=sub_ans key=sub_ans_no}
 // do your code
{/foreach}