How to get pie chart to render in mustache template

How to get pie chart to render in mustache template

by Tom Tom -
Number of replies: 4
How can I get the output renderer of a pie chart from here https://docs.moodle.org/dev/Charts_API#Rendering to render in a mustache template?
Average of ratings: -
In reply to Tom Tom

Re: How to get pie chart to render in mustache template

by Tom Tom -
any thoughts? anyone?
In reply to Tom Tom

Re: How to get pie chart to render in mustache template

by Francis Devine -
Picture of Core developers
The core insights report has an example of rendering charts to templates
https://github.com/moodle/moodle/blob/a31719f91a91f6329752c6d01ba9cb8b1cea68cc/admin/tool/analytics/classes/output/insights_report.php#L113
https://github.com/moodle/moodle/blob/a31719f91a91f6329752c6d01ba9cb8b1cea68cc/admin/tool/analytics/templates/insights_report.mustache

Looks like the process is just rendering the chart to a template variable and then using the {{{ syntax }}} to render it as the raw html

I suspect you could also pull in the core/chart template into your own template
in the template:
 {{#chartobject}} {{> core/chart }} {{/chartobject}}

but your chartobject template variable would need to have a certain layout, from looking at the core output renderer
https://github.com/moodle/moodle/blob/46f977a8bf3c2ba99dc0606e54c119ad4fd27ad4/lib/outputrenderers.php#L4602-L4608

you might need something like

$templatedata->chartobject = ['chartdata' => json_encode($yourchart), 'withtable' => true/false if you want a table];

But I was not able to find any example of this approach being used, so couldn't say if it would work without further testing
In reply to Francis Devine

Re: How to get pie chart to render in mustache template

by Tom Tom -
Thanks for your help. I did get the chart object to render the HTML for a variable. I then used the three curly braces to get it to show up in the template but no luck. The template still escapes the HTML by translating the HTML ankle brackets into their HTML entities equivalents. You can see it in the source--the problem. I will try again with your object specifications about. Thanks again.
In reply to Francis Devine

Re: How to get pie chart to render in mustache template

by Tom Tom -

After changing my code to what you had--pulling in the core chart template as well as clearing out the caches--and only see an inoperable hyperlink ("Show chart data") in my table cell--I switched my code back to what I had ($reportdata['mychart']=$OUTPUT->render($mychart)) and now see the pie chart. I must have forgotten to clear my cache while working on this Friday. That must be the key here. Thanks again for your help.