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

html_options improvement (smarty v2)

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Feb 08, 2011 8:36 pm    Post subject: html_options improvement (smarty v2) Reply with quote

This is an improvement to html_options that allows you to easily add a first option that prompts the user to select something for example "Select a State." This option will have a value of '', and the text can be blank if desired. A prompt is something I do in most dropdowns. You may do this by adding the prompt attribute. Example:

<{html_options options=$states selected=$state_id name='state_id' prompt='Select a State' onchange="$('myForm').onsubmit();"}>

I know I could add it to the array in the PHP, but why have one array for that option list, and one array used for every where else when the only difference is that first value? I prefer to define data once, also this seems more like a display/UI issue rather than a data issue.

I think this could be changed in the core... But here's the code (using v2)



Code:


/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {html_options} function plugin
 *
 * Type:     function<br>
 * Name:     html_options<br>
 * Input:<br>
 *           - 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
 *           - prompt    (optional) - string a first option with an empty value
 * Purpose:  Prints the list of <option> tags generated from
 *           the passed parameters
 * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
 *      (Smarty online manual)
 * @author Monte Ohrt <monte at ohrt dot com>
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
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 = '';
   
    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'name':
                $$_key = (string)$_val;
                break;

            case 'prompt':
                $$_key = (string)$_val;
                break;
           
            case 'options':
                $$_key = (array)$_val;
                break;
               
            case 'values':
            case 'output':
                $$_key = array_values((array)$_val);
                break;

            case 'selected':
                $$_key = array_map('strval', array_values((array)$_val));
                break;
               
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }

    if (!isset($options) && !isset($values))
        return ''; /* raise error here? */

    $_html_result = '';

    if ($prompt) {

    }

    if (isset($options)) {
       
        if ($prompt) {
            smarty_function_html_options_push($options, '', $prompt);
        }
       
        foreach ($options as $_key=>$_val)
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);

    } else {

        if ($prompt) {
            smarty_function_html_options_push($values, '', '');
            smarty_function_html_options_push($output, '', $prompt);
        }
       
        foreach ($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_push(&$array, $new_key, $new_value)
{
   $new_array = array($new_key => $new_value);

   foreach ($array as $key => $value)
   {
      $new_array[$key] = $value;     
   }

   $array = $new_array;
}

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((string)$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;
}

/* vim: set expandtab: */


Last edited by douglassdavis on Tue Feb 08, 2011 9:03 pm; edited 2 times in total
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Feb 08, 2011 8:45 pm    Post subject: Reply with quote

or just do:

Code:
<select name="foo">
  <option value="">--select--</option>
  {html_options options=$options}
</select>
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Feb 08, 2011 8:59 pm    Post subject: Reply with quote

mohrt wrote:
or just do:

Code:
<select name="foo">
  <option value="">--select--</option>
  {html_options options=$options}
</select>



True I could do that, or I could write a foreach statement, but I would rather have smarty write all of the HTML for me. I often also add a name, id, and onchange attribute as well.
Back to top
View user's profile Send private message
alphalycos2
Smarty n00b


Joined: 28 Dec 2012
Posts: 2

PostPosted: Fri Dec 28, 2012 5:04 pm    Post subject: Select/Listbox default option value Reply with quote

How do u make the top value in smarty Mysql listbox selected while using either html options or a select plugin. Thanx
Back to top
View user's profile Send private message Send e-mail
kowach
Smarty Rookie


Joined: 26 Jan 2011
Posts: 13

PostPosted: Sun Dec 30, 2012 5:25 pm    Post subject: Reply with quote

I like concatenation of arrays in this way:

Code:
$options = array(""=>"--select--") + $db->fetchPairs("SELECT id, name FROM some_table");
$smarty->assignByRef('options',$options );


simple and clean Smile
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Sun Dec 30, 2012 5:46 pm    Post subject: Reply with quote

alphalycos2 wrote:
How do u make the top value in smarty Mysql listbox selected while using either html options or a select plugin. Thanx


I'm not sure what you are asking, isn't the top value selected automatically by default?
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Sun Dec 30, 2012 5:47 pm    Post subject: Re: Select/Listbox default option value Reply with quote

kowach wrote:
I like concatenation of arrays in this way:

Code:
$options = array(""=>"--select--") + $db->fetchPairs("SELECT id, name FROM some_table");
$smarty->assignByRef('options',$options );


simple and clean Smile


I see what you are saying. In my view the text that shows up to "Select a Country" or something like that is strictly a display/UI issue, not a data issue, and should be handled in smarty not PHP.

I have had cases where I have tried to add a prompt to the array, but then I need the data for something else and "Select a Country" shows up where it doesn't belong.
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 -> Plugins 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