Renderer

From MoodleDocs
Jump to: navigation, search

A renderer works with Themes to display all of the output for any component of Moodle. This concept was introduced in Moodle 2.0 with the addition of $OUTPUT class and associated /lib php files. There are component and core renderers.

The renderer contains no logic other than what is required to generate the display, and should be compartmentalised into functional chunks. Each chunk should be responsible for producing a widget or control used within the component. This output will vary depending on the component. For example the forum will have a method for displaying a forum post, displaying a thread (which most likely calls the forum post method), and displaying a search form.

A renderer maybe created for a special application such as a plugin.

Overview

A major change in Moodle 2.0 was the addition of the output library ($OUTPUT) that included the introduction of the /lib/outputrenderers.php. These renderers are used as much as possible in Moodle and code is constantly being converted. As Moodle progresses, more of the code should be utilising renderers.

As well as the component renderers there is a core renderer, which is responsible for producing the display for many general things within Moodle, for example tables, action icons, and block regions.

Why use these renderers? They help developers separate the logic and the display when writing code and for theme designers they allow a means by which to to take total control of the HTML that Moodle produces.

$OUTPUT lets themes customise the HTML used for standard things like boxes In addition to moodle_core_renderer, every plugin should also define its own renderer, like moodle_mod_workshop_renderer, and all module-specific HTML output should done in methods of that class. That class should be defined in a files called renderer.php inside your plugin.

Example of calling a renderer

When you actually want to do some output, you need to get an instance of your class (or whatever subclass the theme wants to substitute). For example:

global $PAGE; // If necessary.
$wsoutput = $PAGE->get_renderer('mod_workshop');
echo $wsoutput->manual_allocation_interface($workshop, $allocationdata);

The mod_workshop_renderer object will have access to a moodle_core_renderer available as $this->output. You should use this instead of global $OUTPUT, and you should use it. That is, boxes in the workshop should look like boxes elsewhere, so boxes in the workshop should be output with $this->output->box().

See also

Personal tools
User docs (English)