How to Add a Template to a Block in Moodle: A PHP Developer’s Guide
If you’re a PHP developer working with Moodle, you may have encountered the challenge of adding a template to a block. This is something that seems like it should be a straightforward process, but if you’re new to Moodle or just haven’t worked with templates in blocks before, it can be a bit tricky.
In this blog post, I’ll walk you through how I added a template to a block in Moodle and share the solution to a problem that had me stumped for a little while. This tutorial is especially useful for developers who are building custom plugins or blocks for Moodle and want to learn how to effectively integrate templates.
Setting Up the Block in Moodle
Let’s start by assuming you’ve already created a block in Moodle (or have one like the “Ricoshae Block”) and added it to the dashboard. The goal here is to render content within that block using a template rather than just outputting plain text.
Here’s the initial setup for your block:
$this->content->text = 'Some content';
This is the standard approach for adding content to a block in Moodle, where $this->content->text contains the text you want to display.
Transitioning from Plain Text to Templates
The challenge comes when you want to replace the plain text with a template. My initial thought was that I could simply use the output->render_from_template() method, as I had done in other parts of Moodle. This would allow me to use a template (like default.mustache) and render it directly inside my block.
However, this didn’t work as expected. Why? Because Moodle blocks don’t output content the same way other plugins might. The content inside a block is handled a bit differently.
The Solution: Rendering Content Using render() in the Block
To get the template to render correctly, the solution was simpler than expected. Instead of using output->render_from_template(), which doesn’t work in this context, I used the following approach:
$this->content->text = $OUTPUT->render_from_template('block_rickshaw/default', $data);
Here’s what’s happening:
- The Template: We are calling the template
block_rickshaw/default, which is the name of our default template stored in thetemplatesfolder. - Passing Data: You can also send variables to the template by passing an array of data (
$data). For example, you might want to include dynamic content that’s generated at runtime, like this:
$data = ['some_text' => 'Hello'];
$this->content->text = $OUTPUT->render_from_template('block_rickshaw/default', $data);
This renders the template with the $some_text variable, which can then be used inside your Mustache template.
Don’t Forget Global Settings
One thing I missed at first was that, in addition to rendering the template correctly, you also need to ensure that Moodle is aware of the output object in your global settings. If you don’t set this up, Moodle won’t be able to render anything properly. To fix this, simply make sure that output is registered in the global settings.
You can add it like this:
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('admin');
$PAGE->set_url(new moodle_url('/blocks/rickshaw/index.php'));
$OUTPUT = $PAGE->get_renderer('core');
Final Result
Once you have made these changes, the block will now render using the template. The template could contain things like headings, dynamic content, or other HTML elements.
For example, if you’ve added the following to your Mustache template:
<h1>{{some_text}}</h1>
And you pass some_text = 'Hello', the output on the page will be:
<h1>Hello</h1>
This is how we add a template to a block and pass dynamic content into it!
Recap: Key Steps to Rendering Templates in Moodle Blocks
- Use the
render_from_templatemethod: Instead ofoutput->render(), you need to callrender_from_templateto properly render your template in a block. - Pass data: If you want to send dynamic variables into the template, pass them in an array when calling the render method.
- Ensure global settings are set: You need to ensure that the
outputobject is available in your page setup for rendering.
Conclusion
Adding templates to blocks in Moodle is not as complicated as it first seems, once you know the right steps. By following the steps above, you can easily replace plain text with fully rendered templates, making your Moodle blocks more dynamic and customizable.
If you’re new to Moodle development or PHP, and you want to dive deeper into creating plugins or working with blocks, I offer courses on how to do all of this and more. Check them out in the comments below!
Happy coding, and I hope this post has helped clear up some confusion around Moodle templates.
