mirror of
				https://github.com/smarty-php/smarty.git
				synced 2025-11-03 22:01:36 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			281 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
			
		
		
	
	
			281 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
<?xml version="1.0" encoding="iso-8859-1"?>
 | 
						|
<!-- $Revision$ -->
 | 
						|
<sect1 id="language.function.html.options">
 | 
						|
 <title>{html_options}</title>
 | 
						|
  <para>
 | 
						|
   <varname>{html_options}</varname> is a
 | 
						|
   <link linkend="language.custom.functions">custom function</link>
 | 
						|
   that creates the html <literal><select><option></literal> 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">
 | 
						|
   <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>
 | 
						|
 | 
						|
   <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
 | 
						|
  <literal><select></select></literal> tags are created,
 | 
						|
  otherwise ONLY the <literal><option></literal> list is generated.
 | 
						|
 </para></listitem>
 | 
						|
 | 
						|
  <listitem><para>
 | 
						|
  If a given value is an array, it will treat it as an html
 | 
						|
  <literal><optgroup></literal>, and display the groups.
 | 
						|
  Recursion is supported with <literal><optgroup></literal>.
 | 
						|
 </para></listitem>
 | 
						|
 | 
						|
 <listitem><para>
 | 
						|
  All parameters that are not in the list above are printed as name/value-pairs
 | 
						|
  inside the <literal><select></literal> 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>
 | 
						|
 | 
						|
 | 
						|
 <example>
 | 
						|
  <title>Associative array with the <varname>options</varname> attribute</title>
 | 
						|
  <programlisting role="php">
 | 
						|
<![CDATA[
 | 
						|
<?php
 | 
						|
$smarty->assign('myOptions', array(
 | 
						|
                                1800 => 'Joe Schmoe',
 | 
						|
                                9904 => 'Jack Smith',
 | 
						|
                                2003 => 'Charlie Brown')
 | 
						|
                                );
 | 
						|
$smarty->assign('mySelect', 9904);
 | 
						|
?>
 | 
						|
]]>
 | 
						|
  </programlisting>
 | 
						|
    <para>
 | 
						|
  The following template will generate a drop-down list.
 | 
						|
  Note the presence of the <parameter>name</parameter> attribute
 | 
						|
  which creates the <literal><select></literal> tags.
 | 
						|
  </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="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 <varname>values</varname> and
 | 
						|
<varname>ouptut</varname></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);
 | 
						|
?>
 | 
						|
]]>
 | 
						|
  </programlisting>
 | 
						|
  <para>
 | 
						|
  The above arrays would be output with the following template
 | 
						|
  (note the use of the php <ulink url="&url.php-manual;function.count">
 | 
						|
  <varname>count()</varname></ulink> function as a modifier
 | 
						|
  to set the select size).
 | 
						|
  </para>
 | 
						|
  <programlisting>
 | 
						|
<![CDATA[
 | 
						|
<select name="customer_id" size="{$cust_names|@count}">
 | 
						|
   {html_options values=$cust_ids output=$cust_names selected=$customer_id}
 | 
						|
</select>
 | 
						|
]]>
 | 
						|
  </programlisting>
 | 
						|
  <para>
 | 
						|
   The above example would output:
 | 
						|
  </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>
 | 
						|
</select>
 | 
						|
 | 
						|
]]>
 | 
						|
  </screen>
 | 
						|
 </example>
 | 
						|
  <example>
 | 
						|
   <title>Database example (eg ADODB or PEAR)</title>
 | 
						|
<programlisting role="php">
 | 
						|
<![CDATA[
 | 
						|
<?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));
 | 
						|
 | 
						|
?>
 | 
						|
]]>
 | 
						|
</programlisting>
 | 
						|
<para>
 | 
						|
Where a template could be as follows. Note the use of the
 | 
						|
<link linkend="language.modifier.truncate"><varname>truncate</varname></link>
 | 
						|
modifier.
 | 
						|
</para>
 | 
						|
<programlisting>
 | 
						|
<![CDATA[
 | 
						|
<select name="type_id">
 | 
						|
    <option value='null'>-- none --</option>
 | 
						|
    {html_options options=$contact_types|truncate:20 selected=$contact.type_id}
 | 
						|
</select>
 | 
						|
]]>
 | 
						|
  </programlisting>
 | 
						|
 </example>
 | 
						|
 | 
						|
 <example>
 | 
						|
  <title>Dropdown's with <optgroup> </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>The script above and the following template
 | 
						|
  </para>
 | 
						|
  <programlisting>
 | 
						|
<![CDATA[
 | 
						|
{html_options name=foo options=$myOptions selected=$mySelect}
 | 
						|
]]>
 | 
						|
  </programlisting>
 | 
						|
 | 
						|
  <para>
 | 
						|
   would output:
 | 
						|
  </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>
 | 
						|
</sect1>
 | 
						|
 | 
						|
<!-- Keep this comment at the end of the file
 | 
						|
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
 | 
						|
-->
 |