html_options

属性名必須デフォルト概要
valuesarrayYes(options属性を用いない場合)n/aドロップダウンリストのvalue属性の配列
outputarrayYes(options属性を用いない場合)n/aドロップダウンリストの項目内容の配列
selectedstring/arrayNoemptyあらかじめ選択されているオプション要素
options連想配列Yes(valuesとoutput属性を用いない場合)n/aキーがvalues属性、要素がoutput属性の連想配列
namestringNoemptyselectグループの名前

htmlのドロップダウンリストを作成します。デフォルトで選択されるアイテムも決定できます。必要な属性はvaluesとoutputです(これはoptions属性を使用しない場合)。

配列が渡された場合はhtmlのOPTGROUPとして扱われ、グループが表示されます。 再帰はOPTGROUPによってサポートされます。すべての出力はxhtml互換です。

任意であるname属性が与えられると、 <select name="グループ名"></select> タグによって オプションリストは囲まれます。未指定の場合は、単にオプションリストのみが生成されます。

前述の属性リストに無いパラメータが与えられた場合は、作成された各<select>タグの内側に名前/値のペアで表されます。任意のname属性が与えられない場合には、これらは無視されます。

例 8-8. html_options

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','Carlie Brown'));
$smarty->assign('customer_id', 1001);
$smarty->display('index.tpl');

index.tpl:

<select name=customer_id>
	{html_options values=$cust_ids selected=$customer_id output=$cust_names}
</select>


index.php:

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

index.tpl:

<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>