Files
smarty/docs/en/designers/language-custom-functions/language-function-html-options.xml

278 lines
7.4 KiB
XML
Raw Normal View History

2004-04-13 08:46:28 +00:00
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="language.function.html.options">
<title>{html_options}</title>
2005-09-13 19:03:11 +00:00
<para>
<varname>{html_options}</varname> is a
2005-09-13 19:03:11 +00:00
<link linkend="language.custom.functions">custom function</link>
that creates a html &lt;select&gt;&lt;option&gt; group
with the assigned data. It takes care of which item(s) are selected by
default as well.
</para>
<informaltable frame="all">
<tgroup cols="5">
2005-05-10 11:42:40 +00:00
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Attribute Name</entry>
<entry>Type</entry>
<entry>Required</entry>
<entry>Default</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>values</entry>
<entry>array</entry>
<entry>Yes, unless using options attribute</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>an array of values for dropdown</entry>
</row>
<row>
<entry>output</entry>
<entry>array</entry>
<entry>Yes, unless using options attribute</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>an array of output for dropdown</entry>
</row>
<row>
<entry>selected</entry>
<entry>string/array</entry>
<entry>No</entry>
<entry><emphasis>empty</emphasis></entry>
<entry>the selected option element(s)</entry>
</row>
<row>
<entry>options</entry>
<entry>associative array</entry>
<entry>Yes, unless using values and output</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>an associative array of values and output</entry>
</row>
<row>
<entry>name</entry>
<entry>string</entry>
<entry>No</entry>
<entry><emphasis>empty</emphasis></entry>
<entry>name of select group</entry>
</row>
</tbody>
</tgroup>
</informaltable>
2005-09-13 19:03:11 +00:00
<itemizedlist>
<listitem><para>
Required attributes are
<parameter>values</parameter> and <parameter>output</parameter>,
unless you use the combined <parameter>options</parameter> instead.
</para></listitem>
<listitem><para>
If the optional <parameter>name</parameter> attribute is given, the
&lt;select&gt;&lt;/select&gt; tags are created,
otherwise ONLY the &lt;option&gt;'s list are generated.
</para></listitem>
<listitem><para>
If a given value is an array, it will treat it as an html &lt;optgroup&gt;,
and display the groups. Recursion is supported with &lt;optgroup&gt;.
</para></listitem>
<listitem><para>
2005-05-10 11:42:40 +00:00
All parameters that are not in the list above are printed as
name/value-pairs inside the &lt;select&gt; tag. They are ignored if
the optional <parameter>name</parameter> is not given.
</para></listitem>
<listitem><para>
All output is XHTML compliant.
</para></listitem>
</itemizedlist>
2005-05-10 11:42:40 +00:00
<example>
<title>Associative array with the <varname>options</varname> attribute</title>
<programlisting role="php">
2005-05-10 11:42:40 +00:00
<![CDATA[
<?php
$smarty->assign('myOptions', array(
1800 => 'Joe Schmoe',
9904 => 'Jack Smith',
2003 => 'Charlie Brown')
);
$smarty->assign('mySelect', 9904);
?>
2005-05-10 11:42:40 +00:00
]]>
</programlisting>
<para>
The following template will generate a drop-down list.
Note the presence of the <parameter>name</parameter> attribute
which creates the &lt;select&gt; tags.
2005-05-10 11:42:40 +00:00
</para>
<programlisting>
<![CDATA[
{html_options name=foo options=$myOptions selected=$mySelect}
2005-05-10 11:42:40 +00:00
]]>
</programlisting>
2004-04-13 08:46:28 +00:00
<para>
Output of the above example would be:
</para>
<screen>
<![CDATA[
<select name="foo">
<option label="Joe Schmoe" value="1800">Joe Schmoe</option>
<option label="Jack Smith" value="9904" selected="selected">Jack Smith</option>
<option label="Charlie Brown" value="2003">Charlie Brown</option>
</select>
]]>
</screen>
</example>
<example>
<title>Dropdown with seperate arrays for ouptut.</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty->assign('cust_ids', array(56,92,13));
$smarty->assign('cust_names', array(
'Joe Schmoe',
'Jane Johnson',
'Charlie Brown'));
$smarty->assign('customer_id', 92);
?>
2005-05-10 11:42:40 +00:00
]]>
</programlisting>
<para>
The above arrays would be output with the following template.
Note the sneaky use of the php <ulink url="&url.php-manual;function.count">
<varname>count()</varname></ulink> function as a modifier
to set the select size.
2005-05-10 11:42:40 +00:00
</para>
<programlisting>
<![CDATA[
<select name="customer_id" size="{$cust_names|@count}">
{html_options values=$cust_ids output=$cust_names selected=$customer_id}
</select>
2005-05-10 11:42:40 +00:00
]]>
</programlisting>
<para>
The above example would output:
2005-05-10 11:42:40 +00:00
</para>
<screen>
<![CDATA[
<select name="customer_id">
<option label="Joe Schmoe" value="56">Joe Schmoe</option>
<option label="Jack Smith" value="92" selected="selected">Jane Johnson</option>
<option label="Charlie Brown" value="13">Charlie Brown</option>
2005-05-27 16:25:02 +00:00
</select>
2004-04-13 08:46:28 +00:00
2005-05-10 11:42:40 +00:00
]]>
</screen>
</example>
<example>
<title>Database example (eg ADODB or PEAR)</title>
<programlisting role="php">
<![CDATA[
<?php
2006-03-13 23:13:38 +00:00
$sql = 'select type_id, types from contact_types order by type';
$smarty->assign('contact_types',$db->getAssoc($sql));
2006-03-13 23:13:38 +00:00
$sql = 'select contact_id, name, email, contact_type_id
2005-05-27 16:25:02 +00:00
from contacts where contact_id='.$contact_id;
$smarty->assign('contact',$db->getRow($sql));
?>
]]>
</programlisting>
<para>
where an example template would be
</para>
<programlisting>
<![CDATA[
<select name="type_id">
2005-05-27 16:25:02 +00:00
<option value='null'>-- none --</option>
2006-04-19 10:24:48 +00:00
{html_options options=$contact_types selected=$contact.type_id}
</select>
]]>
</programlisting>
</example>
<example>
<title>Dropdown's with &lt;optgroup&gt; </title>
<programlisting role="php">
<![CDATA[
<?php
$arr['Sport'] = array(6 => 'Golf', 9 => 'Cricket',7 => 'Swim');
$arr['Rest'] = array(3 => 'Sauna',1 => 'Massage');
$smarty->assign('lookups', $arr);
$smarty->assign('fav', 7);
?>
]]>
</programlisting>
<para>With the template would be
</para>
<programlisting>
<![CDATA[
{html_options name=foo options=$myOptions selected=$mySelect}
]]>
</programlisting>
<para>
Output of the above example would be:
</para>
<screen>
<![CDATA[
<select name="breakTime">
<optgroup label="Sport">
<option label="Golf" value="6">Golf</option>
<option label="Cricket" value="9">Cricket</option>
<option label="Swim" value="7" selected="selected">Swim</option>
</optgroup>
<optgroup label="Rest">
<option label="Sauna" value="3">Sauna</option>
<option label="Massage" value="1">Massage</option>
</optgroup>
</select>
]]>
</screen>
</example>
<para>
See also
<link linkend="language.function.html.checkboxes"><varname>{html_checkboxes}</varname></link>
and
<link linkend="language.function.html.radios"><varname>{html_radios}</varname></link>
</para>
2004-04-13 08:46:28 +00:00
</sect1>
2005-05-10 15:09:49 +00:00
2004-04-13 08:46:28 +00:00
<!-- Keep this comment at the end of the file
2005-05-10 15:09:49 +00:00
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->