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

assign php_variable into Javascript function argument in Sm

 
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
gelogelo
Smarty n00b


Joined: 10 Jul 2009
Posts: 2

PostPosted: Fri Jul 10, 2009 7:35 pm    Post subject: assign php_variable into Javascript function argument in Sm Reply with quote

Dear communitarians,

how is it possible to assign a php-variable from php script into a javascript function argument via Smarty->assign("variable', $variable);

i used {literal} and {ldelimit} {rdelimit} and never got the value passed
code within the Smarty-template:

<td width="1%" align="center" valign="left" class="content"><img src="picture.jpg" alt="picture"
onMouseOver="T_func({$variable})" onMouseOut="Unfunc()"></td>

I want to pass the value of $variable to the Javascript-function T_func()

Thanks for your advices!
Back to top
View user's profile Send private message
philoertel
Smarty Rookie


Joined: 25 Jun 2009
Posts: 14
Location: Chicago, IL

PostPosted: Fri Jul 10, 2009 8:57 pm    Post subject: Reply with quote

If all you want is

Code:
<td width="1%" align="center" valign="left" class="content"><img src="picture.jpg" alt="picture"
onMouseOver="T_func({$variable})" onMouseOut="Unfunc()"></td>


what you have there will do it. Did you try it just like that? You don't need {literal} or {ldelimit}. Smarty will turn T_func({$variable}) into T_func(12345) or whatever you assigned to {$variable}. Just be careful what you put in that variable. Unless it's really simple variable like an int or a string with no special chars, use json_encode:

Code:
$smarty->assign('variable', json_encode(array('a' => 1, 'b' => 2)));

_________________
Phil Oertel
Application Developer
http://www.soliantconsulting.com
Back to top
View user's profile Send private message AIM Address
gelogelo
Smarty n00b


Joined: 10 Jul 2009
Posts: 2

PostPosted: Sat Jul 11, 2009 8:09 am    Post subject: SOLVED: smarty assigned variable to Javascript function Reply with quote

Dear phil,

just this is the problem:
Code:
<td width="1%" align="center" valign="left" class="content"><img src="picture.jpg" alt="picture"
onMouseOver="T_func({$variable})" onMouseOut="Unfunc()"></td>

does NOTHING onMouseOver.
Thats the reason i asked.

Infact it's a string ( $variable ) i need to pass to the Javascript function argument (and a tooltip rectangle to be created);
so i tried also : onMouseOver="T_func('{$variable}')"

And now it works!
Thanks for your Help,
Gunter
Back to top
View user's profile Send private message
jLix
Smarty Regular


Joined: 01 Apr 2009
Posts: 59
Location: Lowlands, EU

PostPosted: Mon Jul 13, 2009 12:15 am    Post subject: Reply with quote

TIP: always have a look in the source code of your browser when things are not going according to plan in order to see what the result of the PHP/Smarty parsing looks like. Too often developers only look at their server side code although it is the front-end code, recieved by the browser, that tells the browser what to do.
_________________
http://jlix.net/extensions/smarty
Back to top
View user's profile Send private message Visit poster's website
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