possible to assign data to JS

tommy's Avatar


01 Dec, 2011 02:49 PM

tpl accepts assigned data
if header would present the same behavior it would be possible to send configs for JS classes via fork

  1. Support Staff 1 Posted by tijs on 03 Dec, 2011 09:29 AM

    tijs's Avatar

    Tommy, you can parse a JS file through PHP, in that js-file you can use labels.
    If I want to pass custom data, I parse it in the template in a JSON-object inside scripts tag. And I check in the JS if the data is present.

  2. 2 Posted by tommy on 15 Dec, 2011 09:21 AM

    tommy's Avatar

    i'll give it a try

  3. 3 Posted by tommy on 16 Jan, 2012 04:12 PM

    tommy's Avatar

    could you specify "i parse it in the template in a JSON-object inside scripts tag"?

    A stab in the dark:
    You assign the data to the template (tpl->assign) and create a JS variable in the main template (header.tpl) of the theme. The js is now able to use the JS variable.

    Thanks for your patience :)

  4. 4 Posted by frederik on 16 Jan, 2012 06:22 PM

    frederik's Avatar

    Convert your array to json with json_encode.
    Assign the string to your template.
    Output the string in your template between javascript tags:

    var json_data = {$json_data};

    In your javascript, check for the variable json_data and work with it.

  5. Support Staff 5 Posted by tijs on 16 Jan, 2012 09:31 PM

    tijs's Avatar


  6. tijs closed this discussion on 16 Jan, 2012 09:31 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac