views:

67

answers:

2

i am trying to use smarty variables inside javascript inside tpl

http://pastebin.com/PXmy8jxf

can you suggest me solution please

update: http://pastebin.com/J4ibLz1m

+2  A: 

Simply close the {literal} tag right before inserting the smarty variable, and re-open it again.

Or use {ldelim} and {rdelim} for the pieces of code where you assign values from Smarty.

Alexander
should i need to do it for foreach tag of smarty
soField
No Smarty tag can be placed inside {literal}{/literal}. When you need to place a foreach, a variable, an if, or anything else in Smarty, you must do it outside {literal}. If little JS is present, It might be better to omit the {literal}, and instead use {ldelim} and {rdelim} for literal curly brackets needed in JS.
Alexander
i am really confused , i am trying lots of things but i could not manage to work it, can you please edit code and paste it for me
soField
Actually, your code should have worked. The only real thing I've changed is escaped the single quotes in one place.http://pastebin.com/J4ibLz1mAlso tidied up a bit.
Alexander
And something else. It is a VERY, VERY good practice to generate JSON server-side, using http://www.php.net/manual/en/function.json-encode.php . This way you won't have to escape anything, and you will only have to assign ONCE to a JS variable the JSON, then work with it in pure JS, with JS for, with absolutely no Smarty.
Alexander
i tried your code but it did not work again, there must be strange thing , can we produce this code in different tpl and include it in this tpl ?
soField
ah simple crazy thing , ifound it and it works now , i changed data.addRows(4); to data.addRows(5); thanks
soField
A: 
{literal}
function doSomething(myNumber){
  var result = myNumber/{/literal}{$myDivider}{literal};
  // rest of code...
}
// more functions...
{/literal}

or

{literal}
function doSomething(myNumber){
{/literal}
   var result= myNumber/{$myDivider};
{literal}
  // rest of code...
}
// more functions...
{/literal}

or

function doSomething(myNumber){ldelim}
   var result= myNumber/{$myDivider};
   // rest of code below...
{rdelim}

function doSomeMore(another){ldelim}
   alert('{$myHello}');
   // more code
{rdelim}
karvonen