Page 1 of 1

[solved]passing smarty variable to javascript

Posted: Mon Feb 06, 2006 5:11 pm
by mushi
I just learn smarty and javascript. My question is how to pass a smarty variable/value to javascript.

thanks!

Posted: Mon Feb 06, 2006 6:25 pm
by feyd
smarty stuff normally isn't passed to Javascript, because Javascript is Client-side coding, while Smarty is handled server-side.

Posted: Tue Feb 07, 2006 3:53 am
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. :)

Posted: Tue Feb 07, 2006 11:26 am
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.

Posted: Tue Feb 07, 2006 1:46 pm
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.

Posted: Tue Feb 07, 2006 4:31 pm
by mushi
Thank you!!!