Search code examples
templatesmoodlemustachelms

how to obtain data argument to provide to mustache template in moodle


In moodle (4.0) I have the need to call

$this->output->render_from_template('core_courseformat/local/content/section/cmlist', $sectionData);

From within a renderer. The goal is to render the normal, native, cmlist component in a particular place on the page. But the way that I am currently getting the value of $section does not seem to work. My template renders nothing. I can see from the github source that this template expects data in this format:

Example context (json):
{
    "cms": [
        {
            "cmitem": {
                "cmformat": {
                    "cmname": "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Forum example</span></a>",
                    "hasname": "true"
                },
                "id": 3,
                "module": "forum",
                "extraclasses": "newmessages"
            }
        },
        {
            "cmitem": {
                "cmformat": {
                    "cmname": "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Assign example</span></a>",
                    "hasname": "true"
                },
                "id": 4,
                "module": "assign",
                "extraclasses": ""
            }
        }
    ],
    "hascms": true,
    "showmovehere": true,
    "movingstr": "Moving this activity: folder example",
    "cancelcopyurl": "#",
    "movetosectionurl": "#",
    "strmovefull": "Move 'folder example' to this location"
}
}}

https://github.com/moodle/moodle/blob/1d99ba19a21d57e9f1ed4211a8eeee00e50b7baf/course/format/templates/local/content/section/cmlist.mustache

But here's the challenge. How do I get an object in that format with the data needed to feed the template so it can render the correct CM list items?

Currently I am tring:

$sectionData = get_fast_modinfo($course->id)->get_section_info($section);

But it doesn't seem to return the data structured in the right way.

Any help appreciated.


Solution

  • You can use the function export_for_template of cmlist render class. Something like this:

    $cmlist = new \core_courseformat\output\local\content\section($format, $section);
    $data->cmlist = $cmlist->export_for_template($OUTPUT);
    

    and then send the data to the template. I recommend you imitate the behavior of a moodle as he performs here: https://github.com/moodle/moodle/blob/7ce003b666a66b465ce9335f430a6e4d3535a7f1/course/format/classes/output/local/content/section.php#L223