Précédent   Forum des professionnels en informatique > PHP > Bibliothèques et frameworks > Templates > Smarty
Smarty Forum d'entraide sur le moteur de templates Smarty. Avant de poster -> FAQ Smarty et Cours Smarty
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 06/02/2008, 02h49   #1
Membre éprouvé
 
Avatar de gtraxx
 
Homme Aurélien Gérits
Développeur Web
Inscription : mai 2006
Messages : 1 045
Détails du profil
Informations personnelles :
Nom : Homme Aurélien Gérits
Âge : 30
Localisation : Belgique

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

Informations forums :
Inscription : mai 2006
Messages : 1 045
Points : 498
Points : 498
Par défaut 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 :
{html_options name=foo options=$myOptions selected=$mySelect}
si j'applique un id forcement c'est le select qui l'obtiens
__________________
Au petit déjeuner, je prend du PHP et au souper du jQuery.
AFUP 2009 : Bien optimiser son code pour le référencement
Mon projet open source de librairie PHP 5: magix cjquery
gtraxx est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/02/2008, 07h32   #2
Membre Expert
 
Avatar de eric.pommereau
 
Homme ERIC POMMEREAU
Ingénieur intégration
Inscription : décembre 2004
Messages : 683
Détails du profil
Informations personnelles :
Nom : Homme ERIC POMMEREAU
Âge : 38
Localisation : France

Informations professionnelles :
Activité : Ingénieur intégration
Secteur : Administration - Collectivité locale

Informations forums :
Inscription : décembre 2004
Messages : 683
Points : 1 294
Points : 1 294
Bonjour,

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

Code :
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...

@+
eric.pommereau est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/02/2008, 01h32   #3
Membre éprouvé
 
Avatar de gtraxx
 
Homme Aurélien Gérits
Développeur Web
Inscription : mai 2006
Messages : 1 045
Détails du profil
Informations personnelles :
Nom : Homme Aurélien Gérits
Âge : 30
Localisation : Belgique

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

Informations forums :
Inscription : mai 2006
Messages : 1 045
Points : 498
Points : 498
pas grave je me débrouillerai comme sa, je vais pas faire le difficile alors que smarty est vraiment géniale
__________________
Au petit déjeuner, je prend du PHP et au souper du jQuery.
AFUP 2009 : Bien optimiser son code pour le référencement
Mon projet open source de librairie PHP 5: magix cjquery
gtraxx est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/09/2009, 14h00   #4
Invité de passage
 
Inscription : janvier 2005
Messages : 20
Détails du profil
Informations forums :
Inscription : janvier 2005
Messages : 20
Points : 4
Points : 4
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 :
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")
sohm est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 18h40.


 
 
 
 
Partenaires

Hébergement Web