How to customise the $OUTPUT->main_content() display?

How to customise the $OUTPUT->main_content() display?

by alice springs -
Number of replies: 1

hi all,

I was trying to customise a theme but I got stuck when I wanted to customise the main content area. In the layout file, general.php for example, I noticed that $OUTPUT->main_content() actually output [MAIN CONTENT GOES HERE] instead of the real HTML content. The [MAIN CONTENT GOES HERE] then seemed to be replaced by the proper html after the page is rendered.

What I am trying to do is to do some HTML filtering before outputting the main_content(). I wasn't sure how the software is architectured, can someone advise if customising the $OUTPUT->main_content() is possible and if so, how?

thanks in advance.

Average of ratings: -
In reply to alice springs

Re: How to customise the $OUTPUT->main_content() display?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

To understand why Moodle behaves in the slightly strange way it does now, you have to understand what the code was like in Moodle 1.9, before we tried to introduce the new theme system with layout files. There was only so much we could do, because it had to be a feasible job to convert all the working Moodle 1.9 code to work with Moodle 2.0.

So, what actually happens is that scripts all over Moodle, for example mod/quiz/attempt.php, or login/index.php, or whatever, will do something like

echo $OUTPUT->header();

// ... code to output a lot of HTML goes here.

echo $OUTPUT->footer();

What happens is that $OUTPUT->header(); gets the content of the whole layout file, and returns everything before [MAIN CONTENT GOES HERE]. It stores everything after that marker, so it can later be returned by $OUTPUT->footer();

If you are lucky, the // ... code to output a lot of HTML goes here. bit has actually been re-factored, so the code looks like

$quizrenderer = $PAGE->get_renderer('mod_quiz');
echo $quizrenderer->header();
echo $quizrenderer->attempt_page();
echo $quizrenderer->footer();

and you can apply the techniques of http://docs.moodle.org/dev/Themes_2.0_overriding_a_renderer.

Depending on the type of HTML filtering you want to, you may be able to achieve what you want by creating a Filter plug-in (http://docs.moodle.org/dev/Filters). Basically, that will work if you are only interested in filtering user-input HTML.

If you want to know more about how Moodle was architected, you may find http://www.aosabook.org/en/moodle.html helpful.