XF 2.3 How do I pass an array as a data value to javascript?

stromb0li

Well-known member
How can I pass an array as a data option to javascript?

For example, I have the following in my template:
HTML:
data-my-array="{$myArray}"

In my controller, I'm passing the following param:
PHP:
$viewParams = ['myArray' => [0=>'moo', 1=>'moo1', 2=>'moo2']];

In my js file I have the following:
JavaScript:
options: {
                myArray: null,
            },

However, when I console.log(this.options.myArray); I only see Array or null in the log. I think I need to encode the array via json, but json_encode doesn't seem to be a valid function in the template engine.
 
Last edited:
Back
Top Bottom