You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Moodle 4.0中mustache模板所需的cmlist数据参数

解决Moodle 4.0中cmlist模板数据格式不匹配问题

要获取core_courseformat/local/content/section/cmlist模板所需的正确数据结构,不能直接使用get_section_info返回的对象,需要借助Moodle原生的课程格式输出类来生成符合要求的数据:

  • 首先获取课程的完整modinfo对象,再通过它获取包含活动数据的section对象(而非仅section基础信息)
  • 使用core_courseformat\output\section类的export_for_template方法生成模板所需的结构化数据

具体代码示例:

// 获取课程的完整modinfo数据
$modinfo = get_fast_modinfo($course->id);
// 获取包含活动列表的section对象(第二个参数false表示不忽略隐藏活动)
$section = $modinfo->get_section($sectionnumber, false);
// 实例化原生section输出处理类
$sectionOutput = new \core_courseformat\output\section($course, $section, $modinfo);
// 导出模板需要的结构化数据(包含cms、hascms等字段)
$sectionData = $sectionOutput->export_for_template($this->output);
// 渲染cmlist组件
echo $this->output->render_from_template('core_courseformat/local/content/section/cmlist', $sectionData);

关键说明

get_section_info仅返回section的基础配置信息(如ID、名称、可见性等),而模板需要的cms数组(包含每个活动的渲染数据)、hascms(标记是否有活动)等字段,都是由core_courseformat\output\section类统一处理生成的,这个类会自动将modinfo中的活动数据转换为模板可识别的格式。

如果是自定义课程格式,可继承core_courseformat\output\section类并扩展export_for_template方法,以适配自定义需求。

内容的提问来源于stack exchange,提问作者WillD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 01:55:32