如何在以下场景中使用smarty模板中的变量?

时间:2013-11-28 06:45:56

标签: error-handling django-templates smarty

我的代码如下:

{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_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_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}

您可以在此处看到变量{$page_question_no+1}。 现在我想在以下语句中将此变量用作

{if $question_detail.sub_question_answer_{$page_question_no+1}}

但是它给出了错误,我可否知道正确使用它的正确方法?提前谢谢。

2 个答案:

答案 0 :(得分:0)

试试这个

    {assign var="condition" value="{$question_detail.sub_question_answer}_{$page_question_no+1}"}
    {if $condition}
     // do your code
    {/if}

答案 1 :(得分:0)

这样的事情会起作用:

{$curr_answer = 'sub_question_answer_'|cat:($page_question_no+1)}

{if $question_detail.$curr_answer}