【问题标题】:how to obtain data argument to provide to mustache template in moodle如何获取数据参数以提供给moodle中的胡子模板
【发布时间】:2023-02-17 23:40:38
【问题描述】:

在moodle(4.0)中我需要打电话

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

从渲染器中。目标是在页面的特定位置呈现正常的本机 cmlist 组件。但是我目前获取 $section 值的方式似乎不起作用。我的模板不呈现任何内容。我可以从 github 源代码中看到该模板需要以下格式的数据:

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

但这就是挑战。我如何获得该格式的对象以及提供模板所需的数据,以便它可以呈现正确的 CM 列表项?

目前我正在尝试:

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

但它似乎并没有以正确的方式返回结构化的数据。

任何帮助表示赞赏。

【问题讨论】:

    标签: templates moodle mustache lms


    【解决方案1】:

    您可以使用cmlist渲染类的函数export_for_template。 是这样的:

    $cmlist = new core_courseformatoutputlocalcontentsection($format, $section);
    $data->cmlist = $cmlist->export_for_template($OUTPUT);
    

    然后将数据发送到模板。 我建议你模仿 moodle 的行为,因为他在这里表演: https://github.com/moodle/moodle/blob/7ce003b666a66b465ce9335f430a6e4d3535a7f1/course/format/classes/output/local/content/section.php#L223

    【讨论】:

      【解决方案2】:

      有 cmlist.mustache 的例子吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        • 2016-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多