你能改变moodle中$OUTPUT->main_content()的内容吗?

Can you alter the content of $OUTPUT->main_content() in moodle?

我知道与此相关的类似问题。我的任务比 css 可以处理的任何事情都复杂得多。

例如,在其中一个课程模板中,页面的整个内容区域似乎由 3 行代码处理...

        echo $OUTPUT->course_content_header();
        echo $OUTPUT->main_content();
        echo $OUTPUT->course_content_footer();

假设您需要对课程的内部内容进行一些重大更改。需要重组由 $OUTPUT->main_content()

明确处理的标记、数据等的内容

其他答案追溯到 libs/outputrenderers.php 但如果你仔细研究你会发现:

public function main_content() {
    // This is here because it is the only place we can inject the "main" role over the entire main content area
    // without requiring all theme's to manually do it, and without creating yet another thing people need to
    // remember in the theme.
    // This is an unfortunate hack. DO NO EVER add anything more here.
    // DO NOT add classes.
    // DO NOT add an id.

    return '<div role="main">'.$this->unique_main_content_token.'</div>';
}

...追溯 $this->unique_main_content_token 你也什么也得不到。

在某个地方,在所有这些混乱中,一定有什么东西在组装所有数据和标记。我不仅需要覆盖 child 主题中的 main_content() 函数。给我的只是一张白纸。我需要知道它是如何提取数据来生成页面的,这样我就可以访问课程标题、用户状态等内容。另外,我还可以在它们所属的位置注入我自己的修改,而不是试图干预 css 绝对定位或 post 加载 js 操作。如果必须的话,我会弄脏,但我宁愿先了解系统,然后再将其切成小块。

有谁知道在哪里可以找到 $OUTPUT->main_content() 的 html 的具体处理位置,一般情况下还是针对课程?

Moodle 使用他们所谓的 "renderers"。方法 main_content 是主题指示主要内容在其主题中呈现位置的方式。

Moodle 呈现页面的方式是让开发人员在第一次调用 $OUTPUT->header() 后回显他们想要的任何内容,然后他们必须以 $OUTPUT->footer() 结束输出。在引擎盖下,themer 提供的布局文件被分成两部分,一部分由 $OUTPUT->header() 回应,其余部分由 $OUTOUT->footer().

回应

很遗憾,您将无法捕获所有内容,除非您重写上述方法并引入您自己的输出缓冲。不过,按照惯例,输出应该由渲染器生成,您可以在主题中覆盖它。无论您要寻求哪种解决方案,主题可能都是一个好的开始。

还取决于您要实现的目标,例如更改课程的布局,您最好查看其他插件类型,例如 "Course formats".

希望对您有所帮助。

澄清一下,要覆盖渲染器,您必须创建一个新主题,然后再创建一个渲染器 class。如果你想覆盖超过 core 个渲染器,比如像 mod_scorm 这样的插件渲染器,你必须创建一个特定名称的渲染器 class。

示例:

class theme_yourtheme_core_renderer extends core_renderer { 
    // Overridden core renderer methods here.
}
class theme_yourtheme_mod_scorm_renderer extends mod_scorm_renderer {
    // Overridden scorm renderer methods here.
}

我建议您阅读下面的文档,尽管它可能令人困惑。我还建议您浏览一下其他主题的代码,这些主题在渲染器的使用方面非常有创意。