Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Output API: Difference between revisions

From MoodleDocs
No edit summary
No edit summary
Line 5: Line 5:
See [[ Output Components ]] for (much) older information on the using output components in previous versions of Moodle.  
See [[ Output Components ]] for (much) older information on the using output components in previous versions of Moodle.  


This page is a fresh attempt to explain how renderers, renderables, themes and templates all work together.  
This page is a fresh attempt to explain how renderers, renderables, themes and templates all work together for Moodle 2.9.  


Lets start with building a page that is part of an admin tool.
Lets start with building a page that is part of an admin tool.
Line 11: Line 11:
E.g.  
E.g.  
/admin/tool/demo/index.php
/admin/tool/demo/index.php
<code>
<code php>
   
     // Standard GPL and phpdocs
     // Standard GPL and phpdocs
     require_once(__DIR__ . '/../../../config.php');
     require_once(__DIR__ . '/../../../config.php');
Line 37: Line 38:


Explaining this demo step by step we have firstly the standard config and includes, plus the admin_externalpage_setup('tooldemo') which calls require_login and performs permissions checks for admin pages.
Explaining this demo step by step we have firstly the standard config and includes, plus the admin_externalpage_setup('tooldemo') which calls require_login and performs permissions checks for admin pages.
<code>
<code php>
   
     // Standard GPL and phpdocs
     // Standard GPL and phpdocs
     require_once(__DIR__ . '/../../../config.php');
     require_once(__DIR__ . '/../../../config.php');
Line 46: Line 48:


Then we load the title of the page from a lang string (see [[String_API]] ). We use this string to set some $PAGE properties (url title and heading).  
Then we load the title of the page from a lang string (see [[String_API]] ). We use this string to set some $PAGE properties (url title and heading).  
<code>
<code php>
 
     $title = get_string('pluginname', 'tool_demo');
     $title = get_string('pluginname', 'tool_demo');
     $pagetitle = $title;
     $pagetitle = $title;

Revision as of 02:20, 27 April 2015

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.


Moodle 2.9


See Output Components for (much) older information on the using output components in previous versions of Moodle.

This page is a fresh attempt to explain how renderers, renderables, themes and templates all work together for Moodle 2.9.

Lets start with building a page that is part of an admin tool.

E.g. /admin/tool/demo/index.php

   // Standard GPL and phpdocs
   require_once(__DIR__ . '/../../../config.php');
   require_once($CFG->libdir.'/adminlib.php');
   
   admin_externalpage_setup('tooldemo');
   
   $title = get_string('pluginname', 'tool_demo');
   $pagetitle = $title;
   // Set up the page.
   $url = new moodle_url("/admin/tool/demo/index.php");
   $PAGE->set_url($url);
   $PAGE->set_title($title);
   $PAGE->set_heading($title);
   echo $OUTPUT->header();
   echo $OUTPUT->heading($pagetitle);
   
   $renderable = new \tool_demo\output\index_page('Some text');
   $pluginrenderer = $PAGE->get_renderer('tool_demo');
   echo $pluginrenderer->render($renderable);
   
   echo $OUTPUT->footer();
   

Explaining this demo step by step we have firstly the standard config and includes, plus the admin_externalpage_setup('tooldemo') which calls require_login and performs permissions checks for admin pages.

   // Standard GPL and phpdocs
   require_once(__DIR__ . '/../../../config.php');
   require_once($CFG->libdir.'/adminlib.php');
   
   admin_externalpage_setup('tooldemo');

Then we load the title of the page from a lang string (see String_API ). We use this string to set some $PAGE properties (url title and heading).

   $title = get_string('pluginname', 'tool_demo');
   $pagetitle = $title;
   // Set up the page.
   $url = new moodle_url("/admin/tool/demo/index.php");
   $PAGE->set_url($url);
   $PAGE->set_title($title);
   $PAGE->set_heading($title);

What is $PAGE and where did it come from ?

$PAGE is a global variable used to track the state of the page that is being returned. It is an instance of the moodle_page class defined in lib/pagelib.php.

The most important properties stored in $PAGE are the page context, the url, the layout, title and headings. $PAGE also gives access to some other important classes such as $PAGE->requires, which is an instance of the page_requirements_manager (lib/outputrequirementslib.php). The page_requirements_manager class lets us set dependencies on e.g. javascript and css to be inserted in the correct place in the page (The order things are inserted in the page is hugely important for performance).

$PAGE also lets us load specific renderers for a plugin, or plugin and subtype. We will cover renderers in more detail soon.

TODO...