Smarty提供了一系列的HTML代码生成函数。以下例子均出自Smarty手册,详细说明请参考Smarty中文手册。
html_checkboxes
生成多个多选框
程序:
$this->cust_checkboxes = array(
1000 => 'Joe Schmoe',
1001 => 'Jack Smith',
1002 => 'Jane Johnson',
1003 => 'Charlie Brown'
);
$this->customer_id' = 1001;
模板:
{html_checkboxes name="id" options=$cust_checkboxes checked=$customer_id separator="<br />"}
输出:
<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 />
html_image
生成图片img标签,html_image将自动获取图片长宽。
模板:
{html_image file="pumpkin.jpg"}
输出:
<img src="pumpkin.jpg" alt="" border="0" width="44" height="68" />
html_options
生成多个下拉框选项组,需要自行加上<select>
程序:
$this->cust_options= array(
1000 => 'Joe Schmoe',
1001 => 'Jack Smith',
1002 => 'Jane Johnson',
1003 => 'Charlie Brown'
);
$this->customer_id' = 1001;
模板:
<select name=customer_id>
{html_options options=$cust_options selected=$customer_id}
</select>
输出:
<select name=customer_id>
<option value="1000">Joe Schmoe</option>
<option value="1001" selected="selected">Jack Smith</option>
<option value="1002">Jane Johnson</option>
<option value="1003">Charlie Brown</option>
</select>
html_radios
生成多个单选框
程序:
$this->cust_radios = array(
1000 => 'Joe Schmoe',
1001 => 'Jack Smith',
1002 => 'Jane Johnson',
1003 => 'Charlie Brown'
);
$this->customer_id' = 1001;
模板:
{html_radios name="id" options=$cust_radios checked=$customer_id separator="<br />"}
输出:
<input type="radio" name="id[]" value="1000">Joe Schmoe<br />
<input type="radio" name="id[]" value="1001" checked="checked"><br />
<input type="radio" name="id[]" value="1002">Jane Johnson<br />
<input type="radio" name="id[]" value="1003">Charlie Brown<br />
html_select_date
生成年月日下拉框
模板:
{html_select_date month_format="%m" field_order="YMD" start_year="1950" }
html_select_time 生成时分秒下拉框
{html_select_time use_24_hours=true}
html_table
生成一个表格
程序:
$this->data = array(1,2,3,4,5,6,7,8,9);
模板:
{html_table loop=$data cols=4 }
输出:
<table border="1">
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>9</td><td> </td><td> </td><td> </td></tr>
</table>