Smarty variables inside javascript
I am trying to use smarty variables inside javascript inside tpl
{literal} <script language="javascript"> google.load('visualization','1',{'packages': ['geomap']}); google.setOnLoadCallback(drawMap); function drawMap() { var data = new google.visualization.DataTable(); data.addRows(4); data.addColumn('string', 'Location'); data.addColumn('number', 'Number of links'); {/literal}{foreach from=$last5 item=link name=links key=index} data.setValue({$index},0,'{$link.location|replace:'\'':'\\\''}'); data.setValue({$index},1,{$link.location_count}); {/foreach}{literal} var options = {}; options['dataMode'] = 'regions'; options['region'] = 'world'; var container = document.getElementById('map'); var geomap = new google.visualization.GeoMap(container); geomap.draw(data, options); }; </script> {/literal} can you offer me a solution please
{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} OR (with Smarty 3.x and higher, no literals, etc.)
function doSomething(myNumber){ var result = myNumber/{$myDivider}; // rest of code } In Smarty 3, the left brace with a space character (space, tab or new line) next to it should no longer interact with Smarty logic. Problems resolved using the new version :)
After trying (karvonen) answers [I did not try {rdelim} though, I just tried {literal} ], I had a problem with my ajax requests, it stopped receiving any date from the server when loading after breaking smarty in JS. So, what I did was, I assigned the smarty value to the hidden field value (I know this is not the smartest thing), and then requested that value from JS and therefore assigned it a variable. Hope this helps.