how to pass smarty array to javascript function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manojrana
    New Member
    • Jan 2009
    • 5

    how to pass smarty array to javascript function

    I am using php code in a separately file and html code in as separate file ,
    an array is passed from php to smarty, and then passing that array from smarty to javascript, I am getting it as string'Array',

    Please help me with a solution
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Originally posted by manojrana
    I am using php code in a separately file and html code in as separate file ,
    an array is passed from php to smarty, and then passing that array from smarty to javascript, I am getting it as string'Array',

    Please help me with a solution
    Hi.

    Could you post the structure of you array, and the part of the Smarty template that uses it?

    Usually when something like this happens, you are accidentally printing the array itself, rather than an array element.

    Like, if I passed this PHP array into Smarty:
    [code=php]$names = array("John Doe", "Jane Doe");[/code]
    Simply doing:
    [code=php]{$names}[/code]
    would just print "Array".

    If I wanted to print the values, I would have to pick an index to print, or iterate through them.
    Like:
    [code=php]
    {* Prints "John Doe" *}
    {$names[0]}

    {* Prints all names in the array *}
    {foreach from=$names item=name}
    {$name}<br />
    {/foreach}[/code]

    Comment

    • manojrana
      New Member
      • Jan 2009
      • 5

      #3
      Hi,

      In smarty template
      Now I want to iterate the $names in javascript by using it in template
      I am doing

      [code=html]
      <a href="" class="blink" onClick="getExL oc('~$names`'); ">TEST</a>
      <script type="text/javascript">
      function getExLoc(names)
      {
      alert(names.len gth);
      // its giving array length not the elements number
      // Here I want the elements of the array

      }
      </script>
      [/code]
      Last edited by Atli; Jan 7 '09, 06:48 AM. Reason: Added [code] tags

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Keep in mind that PHP (including the Smarty engine) operates on the server. All the PHP code is executed there, the Smarty template processed, and the response sent to the client browser.
        Once that is done and the browser has received all the output from the server, then the JavaScript is executed by the browser.

        So, directly placing Smarty variables into JavaScript code won't work. You need to use smarty to generate valid JavaScript code.

        Meaning, for example, that if you had an array in Smarty, and you needed to put up a JavaScript alert() for each element, you would have to create an actual JavaScript array, and then have JavaScript use that.

        Like, if this were your Smarty template:
        [code=html]
        <script type="text/javascript">
        var names = [
        {foreach from=$names item=name}
        "{$name}",
        {/foreach}
        ];
        window.onLoad = function() {
        for(var i = 0; i < names.length; i++) {
        alert(names[i]);
        }
        }
        </script>[/code]
        Then your browser would receive this:
        [code=html]
        var names = [
        "John Doe",
        "Jane Doe",
        ];
        window.onload = function() {
        for(var i = 0; i < names.length; i++) {
        alert(names[i]);
        }
        }[/code]
        Which it would then execute as JavaScript and print the two names.
        The browser would never be aware that Smarty, or even PHP for that matter, had anything to do with creating the page it received.

        Comment

        Working...