Silverstripe Version: 4.9
Question: Is there a way to pass a PHP array from controller to Javascript?
Hi, is there a way to pass a whole array from the controller to Javascript? So far I have been using javascriptTemplate to pass variables to JS, but javascriptTemplate only takes either integers or strings. Since my data for the array is taken through a SQL query, it is dynamic and I cannot pass variables as it will be different every time.
Thank you!
$data = [];
foreach ($query as $row) {
array_push($data, [
'ID' => $row['id'],
'Name' => $row['name']
]);
}
$vars = [
"Data" => $data
];
// Here it will give an error "Expected parameter of type 'int[]|string[]', 'array[]' provided"
Requirements::javascriptTemplate("location/file.js", $vars);