Html options
html_options {html_options} is a custom function that creates the html <select><option> group with the assigned data. It takes care of which item(s) are selected by default as well.
Attributes
| Attribute Name | Required | Description |
|---|---|---|
| name | No | Name of select group |
| selectID | No | CSS ID for <select> |
| selectClasses | No | CSS classes for <select> |
| optGroupClasses | No | CSS classes for <optgroup> |
| optionClasses | No | CSS classes for <option> |
| values | Yes, unless using options attribute | An array of values for dropdown |
| output | Yes, unless using options attribute | An array of output for dropdown |
| selected | No | The selected option element(s) as a string or array |
| options | Yes, unless using values and output | An associative array of values and output |
| multiSelection | No | true or false, if required to select multiple option than true (Default false) |
If
multiSelection=true, the following changes will be applied:
- The
<select>element’snameattribute will have[]appended as a suffix. - The
multipleattribute will be added to the<select>element. - You can pass comma-separated values or Array to the
selectedparameter.
- Required attributes are values and output, unless you use the combined options instead.
- If the optional name attribute is given, the
<select></select>tags are created, otherwise ONLY the<option>list is generated. - If a given value is an array, it will treat it as an html
<optgroup>, and display the groups. Recursion is supported with<optgroup>. - All parameters that are not in the list above are printed as name/value-pairs inside the
<select>tag. They are ignored if the optional name is not given. - All output is XHTML compliant.
Examples
<?php
$tplix->assign('myOptions', [
1800 => 'Joe Schmoe',
9904 => 'Jack Smith',
2003 => 'Charlie Brown'
]);
$tplix->assign('mySelect', 9904);The following template will generate a drop-down list. Note the presence of the name attribute which creates the <select> tags.
Where the template is:
{html_options name=foo options=$myOptions selected=$mySelect}The above template will output:
<select name="foo">
<option value="1800">Joe Schmoe</option>
<option value="9904" selected="selected">Jack Smith</option>
<option value="2003">Charlie Brown</option>
</select><?php
$tplix->assign('cust_ids', [56,92,13]);
$tplix->assign('cust_names', [
'Joe Schmoe',
'Jane Johnson',
'Charlie Brown'
]);
$tplix->assign('customer_id', 92);The above arrays would be output with the following template (note the use of the php count() function as a modifier to set the select size).
Where the template is:
<select name="customer_id" size="{$cust_names|@count}">
{html_options values=$cust_ids output=$cust_names selected=$customer_id}
</select>The above template will output:
<select name="customer_id" size="3">
<option value="56">Joe Schmoe</option>
<option value="92" selected="selected">Jane Johnson</option>
<option value="13">Charlie Brown</option>
</select><?php
$arr['Sport'] = array(6 => 'Golf', 9 => 'Cricket',7 => 'Swim');
$arr['Rest'] = array(3 => 'Sauna',1 => 'Massage');
$tplix->assign('lookups', $arr);
$tplix->assign('fav', 7);The above arrays would be output with the following template and create the <optgroup>.
Where the template is:
{html_options name=foo options=$lookups selected=$fav selectID="my-cust-box" selectClasses="form-control" optGroupClasses="opt-cust-grp" optionClasses="option-cust"}The above template will output:
<select name="foo" id="my-cust-box" class="form-control">
<optgroup label="Sport" class="opt-cust-grp">
<option value="6" class="option-grp">Golf</option>
<option value="9" class="option-grp">Cricket</option>
<option value="7" class="option-grp" selected="selected">Swim</option>
</optgroup>
<optgroup label="Rest" class="opt-cust-grp">
<option value="3" class="option-grp">Sauna</option>
<option value="1" class="option-grp">Massage</option>
</optgroup>
</select>