Wednesday, January 4, 2017

Adding renderers and templates to your Moodle plugin - Introduction

If, like me, you're maintaining Moodle plugins that have been around since the early 1.x days, you are constantly planning to modernize and upgrade them. Not doing so, increases the technical debt you will have to endure.

Moodle has been constantly updating its architecture and API's and has completely new ways of dealing with output so that information display can be more easily adapted to changing situations and experiences. The rendering and templating systems are such solutions.

In the old days, our plugins simply mixed the display code with the business logic and output to the screen in any script. There were (and are) core library functions to put output to the screen, such as the "mforms" library, but generally these were used within the plugins logic.

In Moodle 2.0, renderers were introduced to help separate the logic code from the display code and make it easier for themes and theme designers to have things look the way they want them to.

In Moodle 2.9, this system was expanded further to take advantage of templates, using the "Mustache" language. This enabled a much larger separation of the HTML from the PHP/Javascript, allowing display designers to work more directly with what the output will actually look like.

Going forward, you'll want to be using the Moodle template system with renderers to code your display logic.

I'm going to refactor my "questionnaire" plugin to use Moodle renderers and templates as part of a major "modernize" effort for Moodle 3. This will require some significant work as there is display code scattered throughout the various PHP files. For this post, I am going to simplify the work to become familiar with the rendering and templating systems.

I will recreate questionnaire's "index.php" function using renderers and templates as my test bed. This file's output is relatively simple and this will be easy to recreate. Additionally, it is not used directly by core Moodle anywhere, so breaking it won't be a problem to my test site.

My next post will begin the process. If you wish to follow along, get familiar with Moodle's rendering system, templating system as well as autoloading and namespaces.

No comments:

Post a Comment