Skip to content

Mustache Templates

Emil edited this page Dec 9, 2024 · 23 revisions

Using Mustache in Moodle

Mustache is used to display most of the HTML in Moodle.
Mustache templates allow you to write HTML as normal.
Mustache templates are reusable, such that the same template can be imported into other templates.
The templates can include variables from the context which is delivered by Moodles PHP files, in our case these files are the files in the classes/output directory. (See how these ties together in: Pages in the Project).

To understand syntax in Mustache read: Moodle's documentation for templates it gives good examples.

Most used syntax in the project:

Include from context:

{{examplevariable}}

Include from context: without HTML escaping:

{{{examplevariable}}}

Show if variable set:

{{#hasdata}} 
    "some HTML shown if variable is set"
{{/hasdata}}

Show if variable not set

{{^hasdata}}
    "some HTML that is shown if variable is not set"
{{/hasdata}}

Iterate over list:

{{#listdata}} 
    "some HTML shown for every list item"
{{/listdata}}

Commenting:

{{! This is a comment and will not be present in the rendered result. }}

Including Javescript:

{{#js}}
    require(['mod_livequiz/create_question'], (module) => module.init({{quizid}}, {{lecturerid}}, "{{{url}}}" ));
{{/js}}
Clone this wiki locally