I can't get how $PAGE->get_renderer works ...

I can't get how $PAGE->get_renderer works ...

by Gérald CLETTE -
Number of replies: 5
Hi

I'm learning how to develop a new theme in moodle (v3.2) and I've got a problem I can't solve. 

I followed the Ouput API tutorial (https://docs.moodle.org/dev/Output_API) in order to learn how to create a new renderable, templatable component to add it in my theme directory.

 
Here is my project tree in my theme (based on boost) : 

Project tree


I can display my page with this url : http://localhost/theme/ebpcooc/admin/tool/demo/index.php
The problem is in the index.php file. This line makes an error raise on my page : 
     $output = $PAGE->get_renderer('tool_demo');
          Error : Invalid component specified in renderer request

I must admit that I haven't still get the point of component name. 
I've tried a lot of things ... Do I have to give all the directories to the component : "theme_ebpcooc_admin_tool_demo" ? 
Which directories are implicitly added (if any) ? Is it linked to the namespaces ? 

How can I get this renderer in order to render my new component ? 

This could be basic questions but I didn't find any simple answer for the moment : can you please help me ? 

Thanks.

Average of ratings: -
In reply to Gérald CLETTE

Re: I can't get how $PAGE->get_renderer works ...

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Renderers are linked to specific plugins.

So, if you had an admin tool plugin called 'demo' and installed at [site root]/admin/tool/demo with a file renderer.php in that directory, which defines the class 'tool_demo_renderer', then $PAGE->get_renderer('tool_demo') would get an instance of that renderer.

If your theme then wanted to override that renderer, then in your theme's config.php, you should set $THEME->rendererfactory = 'theme_overridden_renderer_factory', then you could create a file renderer.php inside the main directory of your theme. Within your theme's renderer.php, you could create a class theme_MYTHEME_tool_demo_renderer that extends tool_demo_renderer.

Does that help?

In reply to Davo Smith

Re: I can't get how $PAGE->get_renderer works ...

by Gérald CLETTE -

Thank you for your response.

Your solution works and I can see my template displayed in my page.
It's just a sample for the moment but thought that I could add this admin tool in [site root]/theme/[theme name]/admin/tool/demo but it seems to be a mistake. 

If I want to override the render function in my theme_MYTHEME_tool_demo_renderer, I succeeded in using a template (by calling render_from_template). I so would like to create a renderable/templatable class to manage my new widget. Is there a specific directory I have to use ?  

Last thing : you said that "Renderers are linked to specific plugins."
Themes are a specific kind of plugin right ? So I understand that I can get a renderer linked to my theme. 

And yes : that helps ! smile

In reply to Gérald CLETTE

Re: I can't get how $PAGE->get_renderer works ...

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I believe that the convention for renderable/templatable classes is to put them in /[path]/[to]/[plugin]/classes/output/ (and namespace them in [plugin_name]\output). This is a convention rather than a requirement since you create instances of these classes yourself, but its helpful to follow.

Themes are a bit special when it comes to renderers.  A theme won't usually have its own renderer, but instead it will override renderers and templates from other parts of the system.