什么是 Smarty?
为何要使用它?
用例及工作流
语法比较
模板继承
最佳实践
速成课程
{html_radios} 是一个自定义函数,用于创建 HTML 单选按钮组。它还负责处理默认选中的项目。
| 属性名称 | 类型 | 必需 | 默认 | 说明 |
|---|---|---|---|---|
| name | 字符串 | 否 | 单选按钮 | 单选列表名称 |
| values | 数组 | 是,除非使用 options 属性 | 不适用 | 单选按钮值数组 |
| output | 数组 | 是,除非使用 options 属性 | 不适用 | 单选按钮输出数组 |
| selected | 字符串 | 否 | 空 | 选中的单选元素 |
| options | 关联数组 | 是,除非使用 values 和 output | 不适用 | 值和输出的关联数组 |
| separator | 字符串 | 否 | 空 | 用于分隔每个单选项目的文本字符串 |
| assign | 字符串 | 否 | 空 | 将单选标签分配给数组,而不是输出 |
| labels | 布尔值 | 否 | TRUE |
向输出中添加 <label> 标签 |
| label_ids | 布尔值 | 否 | FALSE |
向输出中的 <label> 和 <input> 添加 id 属性 |
| escape | 布尔值 | 否 | TRUE |
转义输出/内容(值始终转义) |
| strict | 布尔值 | 否 | FALSE |
使“额外”属性 disabled 和 readonly 仅在使用布尔 TRUE 或字符串 "disabled" 和 "readonly" 提供时才设置 |
必需属性为 values 和 output,除非你改用 options。
所有输出都符合 XHTML。
以上列表中没有的参数将在创建的每个 <input> 标签内部作为 name/value 对输出。
示例 8.13 {html_radios} 第一个示例
<?php
$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);
?>
其中模板为
{html_radios name='id' values=$cust_ids output=$cust_names
selected=$customer_id separator='<br />'}
示例 8.14 {html_radios} 第二个示例
<?php
$smarty->assign('cust_radios', array(
1000 => 'Joe Schmoe',
1001 => 'Jack Smith',
1002 => 'Jane Johnson',
1003 => 'Charlie Brown'));
$smarty->assign('customer_id', 1001);
?>
其中模板为
{html_radios name='id' options=$cust_radios
selected=$customer_id separator='<br />'}
两个示例都将输出
<label><input type="radio" name="id" value="1000" />Joe Schmoe</label><br /> <label><input type="radio" name="id" value="1001" checked="checked" />Jack Smith</label><br /> <label><input type="radio" name="id" value="1002" />Jane Johnson</label><br /> <label><input type="radio" name="id" value="1003" />Charlie Brown</label><br />
示例 8.15 {html_radios} - 数据库示例(例如 PEAR 或 ADODB)
<?php
$sql = 'select type_id, types from contact_types order by type';
$smarty->assign('contact_types',$db->getAssoc($sql));
$sql = 'select contact_id, name, email, contact_type_id '
.'from contacts where contact_id='.$contact_id;
$smarty->assign('contact',$db->getRow($sql));
?>
从上述数据库分配的变量将使用模板输出
{html_radios name='contact_type_id' options=$contact_types
selected=$contact.contact_type_id separator='<br />'}
另请参阅 {html_checkboxes} 和 {html_options}