Hi,
Has anyone got some sample code for building a renderer for format_base::course_header? I'm finding the short guide included in the functions comments (course/format/lib.php) a little confusing. Any help would be appreciated.
regards,
Cathal.
Yes...and no...
The instructions for this relate to Course Formats, so if you don't know how to make these then it is highly unlikely you would be able to add a renderer.
That said I will move this to Course Formats Forum and hopefully Gareth can help you.
Cheers
Mary
Dear Cathal,
What are you trying to achieve and where? This is because I have only ever known course formats to extend format_base and not themes (which I think you are trying to do given where the post was originally posted). I know themes can override the renderers but I'm not sure about the format_base class hierarchy.
Cheers,
Gareth
Hi Gareth,
Thanks for replying. I'm trying to use course_header() to output some course related content; a title image for each course added through the course summary files setting. I've created my own course format but I'm unsure how to override (if that's the correct term) course_header() for my purpose.
Cathal.
Hi Cathal,
I see. At first glance this should be easy by returning some HTML that would be used when the method is called, but then looking at it seems to need a 'renderable'. I have never done that before so needs investigating.
Gareth
/**
* Course-specific information to be output on any course page (usually above navigation bar)
*
* Example of usage:
* define
* class format_FORMATNAME_XXX implements renderable {}
*
* create format renderer in course/format/FORMATNAME/renderer.php, define rendering function:
* class format_FORMATNAME_renderer extends plugin_renderer_base {
* protected function render_format_FORMATNAME_XXX(format_FORMATNAME_XXX $xxx) {
* return html_writer::tag('div', 'This is my header/footer');
* }
* }
*
* Return instance of format_FORMATNAME_XXX in this function, the appropriate method from
* plugin renderer will be called
*
* @return null|renderable null for no output or object with data for plugin renderer
*/
public function course_header() {
return null;
}
Hi Cathal,
I've been looking at that too and think I've figured it out, need to test though, cannot quite put into words yet, but look at the methods 'course_header' and 'render' in 'lib/outputrenderers.php' and the interface 'renderable' in 'lib/outputcomponents.php'. They helped me to understand what is going on.
Seems an odd way of doing it though as I would have thought that the interface 'renderable' would have a method that the empty class in the example implemented then the render for the format would not have to have a method and it gets called with an instance of an empty class that this then gets discarded. Seems inefficient.
Cheers,
Gareth
Thanks Gareth. I was totally stumped by it.
If you figure it out, would you mind posting some sample code to illustrate how to use it?
Cathal.
Hi Cathal,
I have, please see: https://github.com/gjb2048/moodle-format_topcoll/commit/430872a3e0be8c41bb78723a580ed5f54dd78276
Cheers,
Gareth
Hi Gareth,
Thanks for that. I really appreciate it.
Cathal.