What is the best way to pass JSON data to a template?

Silverstripe Version: 4.3

I’d like to put some charts on a page and have a custom SQL query in my model that returns an array.
I need to convert it to JSON and pass the JSON data either to a ChartJS script or to a template via data attribute.

When I return json_encode($data) in the model, it ends up in the template like this: {"years":[2015,2016,2017,2018], …

Is there any way how to prevent this XML conversion?


You could:

  • add it to the page with Requirements::customScript()
  • add the data to the page using JS templating
  • use $data.RAW in the template
1 Like