How does main_content() works ?

How does main_content() works ?

by Simon Vart -
Number of replies: 1

Hello,


I try to follow-up how does the content is displayed in a page.

Obviously, you have to call $OUTPUT->main_content() to display content.

I checked the function in the code and this part just display a div (which is a hack as the comments states) and put a placeholder into $rendered (which is a big string containing all the HTML of the page, that is cut between header and footer by looking at this placeholder).

But I cannot find where and how this placeholder is replaced by actual content (which looks like populated by plugins, the core renderer contains nothing).

Could someone helps or indicate documentation or general directions / logic behind ?


Regards,

Simon

Average of ratings: -
In reply to Simon Vart

Re: How does main_content() works ?

by Michael Hughes -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
My understanding of this is:

The echo $OUTPUT->main_content(); function doesn't actually emit the main content. It emits a unique token in a div that is used as a marker..

Then

  1. When you included the config.php, lib/setup.php gets included.
  2. This instantiates a moodle_page object which represents the page, and get stored in $PAGE;
  3. A whole load of other stuff happens
  4. Control comes back to *your* page, where you do further things
  5. At some point you call $OUTPUT->header(), which invokes the theme to start laying things out, typically by going to find a file in theme/<theme>/layout/*.php
  6. This "pre-renders" the layout PHP file and stashes it.
  7. You'll see in the header() function there is a bit where it looks for unique main content marker (defined in the theme layout by the $OUTPUT->main-content() call)
  8. The (now) rendered HTML from the layout is then cut in to two pieces, and the 1st part (the header) is dumped out to the browser.
  9. Your page then gets do to it's thing using echo etc.
  10. Then you call $OUTPUT->footer() which sends the 2nd part of the rendered layout file to finish off the page.

This is why (AFAIK) you're meant to do most of your stuff before you call $OUTPUT->header() (like load javascript etc).