Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Sweet function replacement for html_options

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
master_kaos
Smarty Regular


Joined: 02 Aug 2007
Posts: 54

PostPosted: Wed Dec 12, 2007 4:42 pm    Post subject: Sweet function replacement for html_options Reply with quote

I was just about to create my own html_options function that allows you to pass in an array of objects for which you could choose the key and value

for example

Code:

class test
{
   var id;
   var name;
   function test($id, $name)
   {
      $this->id = $id;
      $this->name = $name;
   }
}

$test[] = new test(1, "ABC");
$test[] = new test(2, "DEF");
$test[] = new test(3, "ASDF");
$smarty->assign("test", $test);


SO if I created an array of test objects.
I could then pass this array to HTML objects selected id as key, and name as value, and then output the options.

I thought I would just do a quick google first, and sure enough, someone else already created this a while ago.
I checked the forums, and it doesn't seem like anyone else posted it, so

here is the code!
I am not sure who original author is, so cannot give credit. But I do not take any credit for this code

Install instructions.
Open function.html_options.php
and replace the smarty_function_html_options
with this one. (Leave other 2 functions in there)
Save

Looking over it (and doing testing myself) this is backwards compatible with the array html_options so you should have no issues with the existing ones in your code.


Call this function like so
{html_options name='test' options=$test key_prop='id' val_prop='name'}
Code:

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;

}




I will definitely be using this myself quite a bit
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP