Précédent   Forum du club des développeurs et IT Pro > 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
 
Outils de la discussion
Publicité
'
Vieux 23/05/2007, 05h39   #1
debie1108
Futur Membre du Club
 
Inscription : avril 2004
Messages : 77
Détails du profil
Informations forums :
Inscription : avril 2004
Messages : 77
Points : 19
Points : 19
Par défaut Problème Imbrication Smarty et Javascript

Bonjour ,

J'ai un petit soucis avec un bout de code javacript/smarty.
Voila mon code :

Code :
1
2
3
4
5
6
<select name={$m_product_options[m_product_options][m_product_options2].classtext}{$matching[m_product].productid} onchange="if(form.color{$matching[m_product].productid}.selectedIndex != 0) {literal}{{/literal}
if(form.size{$matching[m_product].productid}.selectedIndex != 0)
{literal}{ {/literal}
{assign var="colorIndex" value=form.color{$matching[m_product].productid}.selectedIndex-1}
{assign var="sizeIndex" value=form.size{$matching[m_product].productid}.selectedIndex-1} 
form.availP{$matching[m_product].productid}.value = optionsArray{$matching[m_product].productid}[{$colorIndex}][{$sizeIndex}]; {literal}}} {/literal}">
Donc bien sur, ca ne marche pas , et voila ce que j'ai quand j'affiche la source :

Code :
1
2
3
4
<select name=Size41 onchange="if(form.color41.selectedIndex != 0) {if(form.size41.selectedIndex != 0){ 
.selectedIndex-1}
.selectedIndex-1} 
form.availP41.value = optionsArray41[form.color{Array[m_product].productid][form.size{Array[m_product].productid]; }} ">

Je comprend pas pourquoi il interprète pas mon code correctement...
Je ne suis pourtant pas en {literal}
Qu'est ce qui cloche ???

Merci mille fois pour votre aide !
Deb
debie1108 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/05/2007, 05h51   #2
is_null
Inscrit
 
Avatar de is_null
 
Djangonaute Linuxien
Inscription : octobre 2006
Messages : 642
Détails du profil
Informations personnelles :
Localisation : Espagne

Informations professionnelles :
Activité : Djangonaute Linuxien

Informations forums :
Inscription : octobre 2006
Messages : 642
Points : 872
Points : 872
Smarty ne considere pas l'imbrication de {, et considere la fin du assign au premier }
Essayez sans les accolades, ou sans grandes conviction, un hack de cat :
Code :
1
2
{assign var="colorIndex" value="form.color"|cat:$matching[m_product].productid|cat: ".selectedIndex-1"}
{assign var="sizeIndex" value="form.size"|cat: $matching[m_product].productid|cat:".selectedIndex-1"}
__________________
Blog / Github / StackOverflow

Citation:
Envoyé par C.A.R. Hoare, The 1980 ACM Turing Award Lecture
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.”
More great quotes - RIP Uriel
is_null est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/05/2007, 06h33   #3
debie1108
Futur Membre du Club
 
Inscription : avril 2004
Messages : 77
Détails du profil
Informations forums :
Inscription : avril 2004
Messages : 77
Points : 19
Points : 19
Bonjour,

Merci beaucoup pour votre réponse.
Effectivement, je suis bête, j'était déjà en "mode smarty" donc j'avais pas besoin de remettre les accolades.
Et j'ai remarqué aussi qu'il n'aimait pas trop l'imbrication des [] et des {}.
Du coup, pour simplifier, j'ai mis mon $matching[m_product].productid dans une variable $pid et du coup, il y a plus de . et de trucs imbriqués, il préfère !!!

Donc cela fonctionne maintenant.

Merci encore pour votre aide.
Deb
debie1108 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/05/2007, 06h41   #4
is_null
Inscrit
 
Avatar de is_null
 
Djangonaute Linuxien
Inscription : octobre 2006
Messages : 642
Détails du profil
Informations personnelles :
Localisation : Espagne

Informations professionnelles :
Activité : Djangonaute Linuxien

Informations forums :
Inscription : octobre 2006
Messages : 642
Points : 872
Points : 872
La concatenation aurait probablement fonctionnée ...
__________________
Blog / Github / StackOverflow

Citation:
Envoyé par C.A.R. Hoare, The 1980 ACM Turing Award Lecture
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.”
More great quotes - RIP Uriel
is_null est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/05/2007, 08h33   #5
debie1108
Futur Membre du Club
 
Inscription : avril 2004
Messages : 77
Détails du profil
Informations forums :
Inscription : avril 2004
Messages : 77
Points : 19
Points : 19
Bonjour,

J'ai encore un soucis et cette fois que crois bien qu'effectivement, je vais devoir recourir à la concaténation mais je sais pas comment faire.

Dans mon onchange j'ai ca :
Code :
onchange="form.m_availP.value = optionsArray{$aid}[{$colorIndex}][{$sizeIndex}];"
Mais il ne l'interprète pas... Il remplace mes {} par leur valeur au lieu de les interpréter. Ex {$aid}=47 il met this.options[this.selectedIndex].value qui est la valeur assignée à âid dans un {assign var...}.
Par contre un alert('{$aid}'); fonctione !!!

Comment faire la concaténation dans ce cadre la ?

J'ai essayé :
Code :
optionsArray|cat:$aid|cat:[|cat:$colorIndex|cat:][|cat:$colorIndex|cat:]);
Mais ca ne fonctionne pas. Dans votre exemple il y avait des guillemet alors qu'ici il n'y en a pas...
J'ai donc essayé ca :
Code :
optionsArray"|cat:$aid|cat:"["|cat:$colorIndex|cat:"]["|cat:$colorIndex|cat:"];
La il me met le code en rouge, et ca ne marche pas non plus...

Je suis un peu perdue avec ce javascript et ce smarty...

Merci de votre aide
Deb
debie1108 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/05/2007, 08h59   #6
debie1108
Futur Membre du Club
 
Inscription : avril 2004
Messages : 77
Détails du profil
Informations forums :
Inscription : avril 2004
Messages : 77
Points : 19
Points : 19
Ok, y'a vraiment des choses que je comprend pas !

alert({$aid}+{$colorIndex});
me donme 471, ce qui est OK !

maintenant

alert(optionsArray+{$aid}+[+{$colorIndex}+][+{$colorIndex}+]);
me donne la même chose que
alert(optionsArray{$aid}[{$colorIndex}][{$colorIndex}]);
C'est à dire ne traduit pas les {$...} : alert(optionsArray+this.options[this.selectedIndex].value+[+form.m_Color.selectedIndex-1+][+form.m_Color.selectedIndex-1+]);
ou alert(optionsArraythis.options[this.selectedIndex].value[form.m_Color.selectedIndex-1][form.m_Color.selectedIndex-1]);

Pourquoi ????
debie1108 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/05/2007, 13h00   #7
is_null
Inscrit
 
Avatar de is_null
 
Djangonaute Linuxien
Inscription : octobre 2006
Messages : 642
Détails du profil
Informations personnelles :
Localisation : Espagne

Informations professionnelles :
Activité : Djangonaute Linuxien

Informations forums :
Inscription : octobre 2006
Messages : 642
Points : 872
Points : 872
value=foo`$bar.foo`bar
Il semble que cela fonctionne, en fait, on place le code a executer par smarty dans des backticks (`).
__________________
Blog / Github / StackOverflow

Citation:
Envoyé par C.A.R. Hoare, The 1980 ACM Turing Award Lecture
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.”
More great quotes - RIP Uriel
is_null est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 25/05/2007, 00h10   #8
debie1108
Futur Membre du Club
 
Inscription : avril 2004
Messages : 77
Détails du profil
Informations forums :
Inscription : avril 2004
Messages : 77
Points : 19
Points : 19
Bonjour,

Ca ne marche pas, enfin, ca fait exactement la même chose...
Voila ce que j'ai écrit :
Code :
1
2
 
form.m_availP.value = optionsArray`$aid`[`$colorIndex`][`$colorIndex`];
Je craque

Merci
Deb
debie1108 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/05/2007, 07h07   #9
is_null
Inscrit
 
Avatar de is_null
 
Djangonaute Linuxien
Inscription : octobre 2006
Messages : 642
Détails du profil
Informations personnelles :
Localisation : Espagne

Informations professionnelles :
Activité : Djangonaute Linuxien

Informations forums :
Inscription : octobre 2006
Messages : 642
Points : 872
Points : 872
Oui mais ce n'est pas de cette facon que smarty permet d'assigner une valeur a une variable a ma connaissance, pourquoi ne pas utiliser {assign} ?
__________________
Blog / Github / StackOverflow

Citation:
Envoyé par C.A.R. Hoare, The 1980 ACM Turing Award Lecture
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.”
More great quotes - RIP Uriel
is_null est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/05/2007, 10h31   #10
debie1108
Futur Membre du Club
 
Inscription : avril 2004
Messages : 77
Détails du profil
Informations forums :
Inscription : avril 2004
Messages : 77
Points : 19
Points : 19
C pas une variable smarty, c'est une variable javscript en fait.
C'est justement ca qui doit bloquer.... La mixage de varaible javascript et smarty...

Ca devient compliqué....

Une idée ?
debie1108 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/05/2007, 11h20   #11
is_null
Inscrit
 
Avatar de is_null
 
Djangonaute Linuxien
Inscription : octobre 2006
Messages : 642
Détails du profil
Informations personnelles :
Localisation : Espagne

Informations professionnelles :
Activité : Djangonaute Linuxien

Informations forums :
Inscription : octobre 2006
Messages : 642
Points : 872
Points : 872
Bien sur, les backticks ne fonctionnent qu'entre des balises smarty ...
__________________
Blog / Github / StackOverflow

Citation:
Envoyé par C.A.R. Hoare, The 1980 ACM Turing Award Lecture
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.”
More great quotes - RIP Uriel
is_null est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/05/2007, 02h22   #12
debie1108
Futur Membre du Club
 
Inscription : avril 2004
Messages : 77
Détails du profil
Informations forums :
Inscription : avril 2004
Messages : 77
Points : 19
Points : 19
Mais je peux pas faire autrement mon tableau c'est en javascript...
Faudrait que je crée un tableau smarty dans le template. C'est possible ca ?
On fait comment ?

Un truc du style {assign var=tab[1][2] value=form.selectedIndex.value} ?
et après : form.monInput.value = {$tab[1][2]}; ?
debie1108 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/05/2007, 03h59   #13
debie1108
Futur Membre du Club
 
Inscription : avril 2004
Messages : 77
Détails du profil
Informations forums :
Inscription : avril 2004
Messages : 77
Points : 19
Points : 19
Re-bonjour,

J'ai résolu le problème en passant tout en javascript comme ca plsu de soucis d'interférence entre javascript et smarty.
Au lieu d'assigner un variable smarty $aid, j'assigne un varibale javascript var aid et je concatene le tout et ca amrche....

Merci de votre aide is_null.
A bientot
debie1108 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 30/12/2012, 15h12   #14
Ben Gates
Invité régulier
 
Inscription : juillet 2009
Messages : 20
Détails du profil
Informations forums :
Inscription : juillet 2009
Messages : 20
Points : 7
Points : 7
Hello,

Désolé de remonter un vieux topic mais pour ceux (comme moi) qui tombent dessus via une recherche google, voici quelques explications sur Smarty et Javascript :

http://www.couscousboulette.com/2012...et-javascript/

Avec Smarty v3, c'est désormais beaucoup plus simple ; et il est désormais possible d'encapsuler du Smarty dans du Smarty, rendant ce type de code valide par exemple :
Code :
{assign var="colorIndex" value=$form.{$color}}
En outre, quelques raccourcis rapprochent maintenant la syntaxe Smarty de la syntaxe PHP :
Code :
{$colorIndex = $form.{$color}}
Beaucoup plus intuitif que le "assign"
Ben Gates est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 11h45.


 
 
 
 
Partenaires

Hébergement Web