"html_options" for dealing with objects and arrays of objects

From: Date: Fri, 30 May 2003 18:09:57 +0000
Subject: "html_options" for dealing with objects and arrays of objects
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-9613@lists.php.net to get a copy of this message
I recently had the need to create a set of select lists that met the following criteria: 1. From an object where all property names became labels and property values as values. 2. From an array of objects and using from each object one of the properties as the key and one of the property names as the value. 3. As a direct result from the above, from an array of arrays, where the key/value pairs are stored in the same index of each array in the master array. Seeing an oportunity to expand the existing html_options plugin, I came up with the following solution, which I hope will be merged into the existing html_options function for the next smarty release (how should I go about making this happen?). Some example code ----------------------------- A simple object passed in as an option: {html_options_ob options=$object} An array of record objects, with at least two properties: {html_options_ob options=$records key_prop='record_id' val_prop='name'} A single object from the $records array, passed as an object { html_options_ob options=$records.432 } This last example produced the following html options: <option label="record_id" value="432">record_id</option> <option label="type_id" value="132">type_id</option> <option label="type_name" value="menu">type_name</option> <option label="name" value="Add New Record">name</option> <option label="description" value="new record">description</option> In addition, I added a 'multiple' option, to generate a multiple select list. This can be augmented by adding size='5'. {html_options_ob options=$object multipl='1' size='5'} Z. ========================================== New Function ========================================== <?php /* * Smarty plugin * ------------------------------------------------------------- * Type: function * Name: html_options * Input: name (optional) - string default "select" * values (required if no options supplied) - array * options (required if no values supplied) - associative array * selected (optional) - string default not set * output (required if not options supplied) - array * multiple (optional) mutiple=<anything>, creates a multiple select list * key_prop (optional) when passing an array of objects for $options * val_prop (required when passing an array of objects for $options) * * Purpose: Prints the list of <option> tags generated from * the passed parameters from an array of objects. * ------------------------------------------------------------- */ function smarty_function_html_options($params, &$smarty) { require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); $name = null; $values = null; $options = null; $selected = array(); $output = null; $extra = ''; //added for dealing with array of objects for options $pKey = null; $pValue = null; foreach($params as $_key => $_val) { switch($_key) { case 'name': $$_key = (string)$_val; break; case 'options': if ( is_object($_val) ) //leave as an object $$_key = (object)$_val; else $$_key = (array)$_val; break; case 'selected': case 'values': case 'output': $$_key = array_values((array)$_val); break; case 'key_prop': $pKey = (string)$_val; break; case 'val_prop': $pValue = (string)$_val; break; case 'multiple': $extra .= ' '.$_key . ''; default: $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; break; } } if (!isset($options) && !isset($values)) return ''; /* raise error here? */ $_html_result = ''; if (is_array($options)) { while ( list($_key, $_val) = each($options) ){ if ( is_object($_val) ) { if ( !$pValue ) return; //required for an array of objects. $_objvars = get_object_vars((object)$_val); if ( $pKey ) { $_key = $_objvars[$pKey]; } $_val = $_objvars[$pValue]; } if ( is_array($_val) ) { if ( !$pValue ) return; //required for an array of arrays. if ( $pKey ) { $_key = $_val[$pKey]; } $_val = $_val[$pValue]; } $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); } } elseif(is_object($options)) { $_objvars = get_object_vars($_val); while ( list($_val, $_key) = each($_objvars) ) { $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); } } else { foreach ((array)$values as $_i=>$_key) { $_val = isset($output[$_i]) ? $output[$_i] : ''; $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); } } if(!empty($name)) { $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n"; } return $_html_result; } function smarty_function_html_options_optoutput($key, $value, $selected) { if(!is_array($value)) { $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' . smarty_function_escape_special_chars($key) . '"'; if (in_array($key, $selected)) $_html_result .= ' selected="selected"'; $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n"; } else { $_html_result = smarty_function_html_options_optgroup($key, $value, $selected); } return $_html_result; } function smarty_function_html_options_optgroup($key, $values, $selected) { $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n"; foreach ($values as $key => $value) { $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected); } $optgroup_html .= "</optgroup>\n"; return $optgroup_html; } ?>

« previous php.smarty.general (#9613) next »