4

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 suggest me a solution please

3
  • 1
    What is your question? What doesn't work? Commented Aug 4, 2010 at 10:55
  • this doesnt work, is it syntactically correct ? Commented Aug 4, 2010 at 10:58
  • -1 --- Your link expired - this really makes this post unusuable for me! Commented Oct 5, 2010 at 11:32

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

7 Comments

should i need to do it for foreach tag of smarty
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.
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
Actually, your code should have worked. The only real thing I've changed is escaped the single quotes in one place. pastebin.com/J4ibLz1m Also tidied up a bit.
And something else. It is a VERY, VERY good practice to generate JSON server-side, using 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.
|
7
{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 above, no literals etc necessary)

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

In Smarty 3 a left curly brace with a space character (space, tab or newline) next to it should not mess with the Smarty logic any more. Problems solved by using the new version :)

3 Comments

I've a similar situation here but little complex, look @ my code. {literal}<iframe src="http://www.facebook.com/plugins/registration.php?client_id=2343434234&fb_only=true&redirect_uri=http://localhots/POC/manage_users.php?reg_type={/literal}{$regtype}{literal}&act_action=save_profiledata&fields=[{'name':'name'}, {'name':'email'}, {'name':'gender'}, {'name':'location'}, {'name':'birthday'}]" scrolling="auto" frameborder="no" style="border:none" allowTransparency="true" width="100%" height="360"></iframe>{/literal} In this case it doesn't return full url but the value is retrieved.Any help?
Well, at least you have "localhots", not "localhost". Moreover, 'localhost' may be unacceptable, see stackoverflow.com/questions/3707738/…
This approach {/literal}{$myDivider}{literal} worked out for me.
0

After trying (karvonen) answers [I did not try {rdelim} though, only tried {literal}], I got a problem with my ajax requests, it stopped fetching any date from server on loading after the smarty breaking in JS. So, what I did is I assigned the smarty value to a hidden field (I know this is not most smart thing to do) and then requested that value from JS and hence assigned it to a variable. Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.