[solved]passing smarty variable to javascript

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
mushi
Forum Newbie
Posts: 8
Joined: Mon Jun 27, 2005 1:34 pm

[solved]passing smarty variable to javascript

Post by mushi »

I just learn smarty and javascript. My question is how to pass a smarty variable/value to javascript.

thanks!
Last edited by mushi on Thu Feb 09, 2006 1:19 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

smarty stuff normally isn't passed to Javascript, because Javascript is Client-side coding, while Smarty is handled server-side.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Actually it can be and we have done it alot. You just need to use the Literal tag from smarty.

Here is a bit of javascript from our game.

Code: Select all

// changeDelta function //
{literal}
<SCRIPT LANGUAGE="JavaScript">
<!--
function changeDelta(desiredvalue,currentvalue)
{
	Delta=0; DeltaCost=0;
	Delta = desiredvalue - currentvalue;

	while (Delta>0) 
	{
		DeltaCost=DeltaCost + Math.pow({/literal}{$upgrade_factor}{literal},desiredvalue-Delta);
		Delta=Delta-1;
	}

	DeltaCost=DeltaCost * {/literal}{$upgrade_cost}{literal} * {/literal}{$alliancefactor}{literal};
	return DeltaCost;
}
// -->
</SCRIPT>

{/literal}
When you want to insert a smarty variable into the javascript you can wrap the javascript inside a literal tag and then anywhere you would like to insert a smarty variable just use the example above for how to do it.

This isn't the only way you can do it but it does work. :)
mushi
Forum Newbie
Posts: 8
Joined: Mon Jun 27, 2005 1:34 pm

Post by mushi »

I tried the literal tag, but it's giving me an error. I'm not very familiar with javascript, so I have no idea what it mean.

Error: missing: after property id.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Well, usually it will give you a line the error occured on in the java script. You can then view the source for the page and see where the error occured and that should help you figure out how to fix it.
mushi
Forum Newbie
Posts: 8
Joined: Mon Jun 27, 2005 1:34 pm

Post by mushi »

Thank you!!!
Post Reply