IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[Smarty] paramètres supplémentaires pour html_options


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre confirmé Avatar de gtraxx
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2006
    Messages
    1 043
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2006
    Messages : 1 043
    Points : 570
    Points
    570
    Par défaut [Smarty] paramètres supplémentaires pour html_options
    Bonsoir tous le monde, je me demandais si cétais possible d'ajouter un paramètre id ou class sur les options en utilisant {$html options}.
    J'ai bien passez tous ce que je voulais mais j'aurai aimez ajouter un id sur les options qui servira pour ma fonction javascript.
    Dans la doc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    {html_options name=foo options=$myOptions selected=$mySelect}
    si j'applique un id forcement c'est le select qui l'obtiens

  2. #2
    Membre chevronné
    Avatar de eric.pommereau
    Homme Profil pro
    Ingénieur, pôle cartographie
    Inscrit en
    Décembre 2004
    Messages
    715
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur, pôle cartographie
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Décembre 2004
    Messages : 715
    Points : 1 790
    Points
    1 790
    Par défaut
    Bonjour,

    A priori l'injection d'ID n'est pas prévue, fichier smarty/plugin/function.html_options.php :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    ...
        foreach($params as $_key => $_val) {
            switch($_key) {
                case 'name':
                    $$_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;
            }
        }
    ...
    Donc le plus raisonnable selon moi serait de recréer un pugin genre my_html_option dans lequel ton option (l'ajout d'ID) serait ajoutée...

    @+

  3. #3
    Membre confirmé Avatar de gtraxx
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2006
    Messages
    1 043
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2006
    Messages : 1 043
    Points : 570
    Points
    570
    Par défaut
    pas grave je me débrouillerai comme sa, je vais pas faire le difficile alors que smarty est vraiment géniale

  4. #4
    Membre à l'essai
    Inscrit en
    Janvier 2005
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Janvier 2005
    Messages : 20
    Points : 12
    Points
    12
    Par défaut Code
    Salut
    J'ai eu le même besoin et j'ai fait la petite modif
    donc au cas où une âme passe et a le même besoin
    voici le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    <?php
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */
     
     
    /**
     * Smarty {html_options_gpm} 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
     *           - gpm        {optional) - the key selected
     * 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_gpm($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 'classnames':
                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 (isset($options)) {
     
            foreach ($options as $_key=>$_val)
                $_html_result .= smarty_function_html_options_optoutput_gpm($_key, $_val, $selected, $classnames);
     
        } else {
     
            foreach ($values as $_i=>$_key) {
                $_val = isset($output[$_i]) ? $output[$_i] : '';
                $_html_result .= smarty_function_html_options_optoutput_gpm($_key, $_val, $selected, $classnames);
            }
     
        }
     
        if(!empty($name)) {
            $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
        }
     
        return $_html_result;
     
    }
     
    function smarty_function_html_options_optoutput_gpm($key, $value, $selected, $classnames) {
        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"';
            if ($classnameid = array_key_exists((string)$key, $classnames))
                $_html_result .= ' class="' . $classnames[$classnameid] . '"';
            $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
        } else {
            $_html_result = smarty_function_html_options_optgroup_gpm($key, $value, $selected);
        }
        return $_html_result;
    }
     
    function smarty_function_html_options_optgroup_gpm($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_gpm($key, $value, $selected);
        }
        $optgroup_html .= "</optgroup>\n";
        return $optgroup_html;
    }
     
    /* vim: set expandtab: */
     
    ?>
    la modif est au niveau de l'attribut classnames qui est un tableau associatif contient la liste des id ayant une class, et il donne la liste des classes

    ex :
    array(1 => "classmachin")

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 02/09/2011, 12h47
  2. paramètres supplémentaires pour load d'une formPanel
    Par kimlee dans le forum Ext JS / Sencha
    Réponses: 2
    Dernier message: 26/05/2011, 17h05
  3. [Sybase] paramètre facultatif pour procédure stockée
    Par MashiMaro dans le forum Sybase
    Réponses: 6
    Dernier message: 15/12/2004, 15h14
  4. Réponses: 5
    Dernier message: 18/11/2004, 17h19
  5. Paramètres personnalisés pour l'installation windows XP
    Par SkyDev dans le forum Windows XP
    Réponses: 5
    Dernier message: 04/11/2004, 12h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo