html_checkboxes

Attribute Name Type Required Default Description
name string No checkbox name of checkbox list
values array Yes, unless using options attribute n/a an array of values for checkbox buttons
output array Yes, unless using options attribute n/a an array of output for checkbox buttons
selected string/array No empty the checked checkbox element(s)
options associative array Yes, unless using values and output n/a an associative array of values and output
separator string No empty string of text to separate each checkbox item
labels boolean No true add <label>-tags to the output

属性 类型 是否必须 缺省值 描述
name string No checkbox 复选按钮组的名称
values array Yes, 或指定 options 属性 n/a 包含复选按钮组值的数组
output array Yes, 或指定 options 属性 n/a 包含复选按钮组显示值的数组
selected string/array No empty 已选定的元素或元素数组
options associative array Yes,或指定 values 属性 n/a 包含值和显示的关联数组
separator string No empty 分隔每个复选按钮的字符串
labels boolean No true 是否为每个复选按钮添加 <label> 标签

html_checkboxes is a custom function that creates an html checkbox group with provided data. It takes care of which item(s) are selected by default as well. Required attributes are values and output, unless you use options instead. All output is XHTML compatible.

自定义函数 html_checkboxes 根据给定的数据创建复选按钮组. 该函数可以指定哪些元素被选定. 要么必须指定 values 和 ouput 属性,要么指定 options 替代. 所有的输出与 XHTML 兼容.

All parameters that are not in the list above are printed as name/value-pairs inside each of the created <input>-tags.

上表未提到的其它参数在 <input> 标签中以"名称/属性"对的方式显示.

Example 8-6. html_checkboxes
Smarty手册范例 8-6. html_checkboxes 函数演示

index.php:

require('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('cust_ids', array(1000,1001,1002,1003));
$smarty->assign('cust_names', array('Joe Schmoe','Jack Smith','Jane Johnson','Charlie Brown'));
$smarty->assign('customer_id', 1001);
$smarty->display('index.tpl');


index.tpl:

{html_checkboxes values=$cust_ids checked=$customer_id output=$cust_names separator="<br />"}


index.php:

require('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('cust_checkboxes', array(
			1000 => 'Joe Schmoe',
			1001 => 'Jack Smith',
			1002 => 'Jane Johnson',
			1003 => 'Charlie Brown'));
$smarty->assign('customer_id', 1001);
$smarty->display('index.tpl');

index.tpl:

{html_checkboxes name="id" options=$cust_checkboxes checked=$customer_id separator="<br />"}


输出结果: (both examples)

<label><input type="checkbox" name="checkbox[]" value="1000" />Joe Schmoe</label><br />
<label><input type="checkbox" name="checkbox[]" value="1001" checked="checked" />Jack Smith</label><br />
<label><input type="checkbox" name="checkbox[]" value="1002" />Jane Johnson</label><br />
<label><input type="checkbox" name="checkbox[]" value="1003" />Charlie Brown</label><br />