1

我将一个名为 $users_criteria 的数组分配给 smarty 模板。数组如下:

$users_criteria = array(
          1 => array(
                 'label' => 'Registered users',
                 'condition' => 'date_range'
               ),
          2 => array(
                 'label' => 'Logged-in users',
                 'condition' => 'date_range'
               ),
          3 => array(
                 'label' => 'Not logged-in Users',
                 'condition' => 'date_range'
               ),
          4 => array(
                 'label' => 'Never logged-in Users'
               ),
          5 => array(
                 'label' => 'Package purchase',
                 'condition' => 'package_type'
               ),
          6 => array(
                 'label' => 'Try demo packages',
                 'condition' => 'demo_packages'
               )
        ); 
$smarty->assign('users_criteria', $users_criteria);

        $file_to_show = 'contact-list-import.tpl';

现在模板中有六个复选框,我想将上述数组中的标签值分配给每个复选框的 value 属性。也就是说,每个复选框都应该有来自上述数组的单个值。简而言之,我的要求是:上述数组中值为 1 的键应映射到名称为 registered_users 的复选框,上述数组中值为 2 的键应映射到名称为 logged_in_users 的复选框,...以这种方式类似到所有六个复选框。我试图实现这一点,但我得到的复选框的重复组合。我的代码如下:

{foreach from=$users_criteria key=myId item=criteria}
                  <tr height="30">
                    <td align="left" width="20%">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="registered_users" value="{$criteria.label}"/>Registered users &nbsp;&nbsp;  
                    </td>
                    <td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
                    <td align="left" >&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="registered_users_from_date" maxlength="10" width="20%"></td>
                    <td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
                    <td>&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="registered_users_to_date" maxlength="10" width="20%"></td> 
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td align="left" width="20%">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="logged_in_users" value="{$criteria.label}"/>Logged-in users &nbsp;&nbsp;  
                    </td>
                    <td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
                    <td align="left" >&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="logged_in_users_from_date" maxlength="10"></td>
                    <td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
                    <td>&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="logged_in_users_to_date" maxlength="10"></td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td align="left" width="20%">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="not_logged_in_users" value="{$criteria.label}"/>Not logged-in Users &nbsp;&nbsp;  
                    </td>
                    <td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
                    <td align="left" >&nbsp;<input type="text" style="width:100px;" class="inputfield" name="not_logged_in_users_from_date" id="not_logged_in_users_from_date" maxlength="10"></td>
                    <td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
                    <td>&nbsp;<input type="text" style="width:100px;" class="inputfield" name="not_logged_in_users_to_date" id="not_logged_in_users_to_date" maxlength="10"></td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td width="300" colspan="5">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="never_logged_in_users" value="{$criteria.label}"/>Never logged-in Users  
                    </td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td width="300" colspan="1">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="package_purchase" value="{$criteria.label}"/>Package purchase  
                    </td>
                    <td align="left"  colspan="4">
                      <select name="test_pack_type_id" id="test_pack_type_id">
                      <option value="">All</option> 
                      {if $all_type}
                       {foreach from=$all_type item="type"}
                      <option value="{$type.package_type_id}">{$type.package_type_name}</option>
                       {/foreach}
                      {/if}
                 </select>
                  </td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td width="300" colspan="5">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="try_demo_packages" value="try_demo_packages"/>Try demo packages  
                    </td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  {/foreach}

您可以更正代码以便在 smarty 中使用 foreach 为复选框分配正确的值吗?提前致谢。

4

1 回答 1

2

你可以使用{section}标签来做到这一点。我给你举个例子。

 {section name=i loop=$users_criteria}
    <tr height="30">
        <td align="left" width="20%">
        <input type="checkbox" class="user_checkbox" name="user_checkbox" id="registered_users" value="{$users_criteria[i].label}"/>Registered users &nbsp;&nbsp;  
        </td>
        <td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
        <td align="left" >&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="registered_users_from_date" maxlength="10" width="20%"></td>
        <td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
        <td>&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="registered_users_to_date" maxlength="10" width="20%"></td> 
    </tr>
    <tr><td colspan="5">&nbsp;</td></tr>
 {/section}
于 2013-08-01T07:26:33.430 回答