2013-11-28 47 views
0

我的代碼如下:如何在以下場景中使用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}} 

,但它給錯誤,我可以知道正確使用它的正確方法是什麼?提前致謝。

+0

你試過'{如果$ question_detail.sub_question_answer _($ page_question_no + 1)}' – suneesh

回答

0

試試這個

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

喜歡的東西,將工作:

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

{if $question_detail.$curr_answer} 
相關問題