Skip to content

Commit 163e069

Browse files
committed
[docs] renderer helper for DI
1 parent 8fee601 commit 163e069

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/apis/subsystems/output/index.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,40 @@ echo $output->footer();
106106

107107
This prints the HTML for the bottom of the page. It is very important because it also prints out things that were added to the `page_requirements_manager` and that need to be printed in the footer; things like JavaScript includes, navigation tree setup, closing open containers tags etc. The reason all JavaScripts are added to the footer of the page is for performance. If you add JavaScript includes to the top of the page, or inline with the content, the browser must stop and execute the JavaScript before it can render the page. See https://developers.google.com/speed/docs/insights/BlockingJS for more information.
108108

109+
### Accessing renderers with dependency injection
110+
111+
In the example above, we used `$PAGE->get_renderer('tool_demo')` to get an instance of the renderer. This is the traditional way to get a renderer. However, if you are in a class designed for dependency injection, the use of global variables is discouraged. You can read more about dependency injection in the [Dependency Injection](../../../concepts/dependency-injection/index.md) guide.
112+
113+
Instead, you can use the `core\output\renderer_helper` class to get any renderer instance without using the global. This is an example of how to use the `renderer_helper` class to get a renderer instance:
114+
115+
```php
116+
class my_di_example {
117+
public function __construct(
118+
/** @var \core\output\renderer_helper $rendererhelper the renderer helper */
119+
protected readonly \core\output\renderer_helper $rendererhelper,
120+
) {
121+
}
122+
123+
public function do_something_with_my_renderer() {
124+
/** @var \tool_demo\output\renderer $renderer */
125+
$renderer = $this->rendererhelper->get_renderer('tool_demo');
126+
// Do something with the renderer.
127+
}
128+
129+
public function do_something_with_core_renderer() {
130+
// For convenience, the renderer helper also provides a method to get the core renderer.
131+
$renderer = $this->rendererhelper->get_core_renderer();
132+
// Do something with the core renderer.
133+
}
134+
}
135+
```
136+
137+
:::note
138+
139+
The `core\output\renderer_helper` class serves as a wrapper around the global `$PAGE` object. This is necessary because the `$PAGE` object can change during script execution, making direct injection impossible. Dependency injection relies on singletons, and the `renderer_helper` class provides a workaround for this limitation.
140+
141+
:::
142+
109143
### Renderable
110144

111145
In the code above, we created a renderable. This is a class that you have to add to your plugin. It holds all the data required to display something on the page. Here is the renderable for this example:

0 commit comments

Comments
 (0)