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 are smart as they allow you to write HTML as normal, but you can also include things 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.

Syntax quick reference:

Include from context:

{{examplevariable}}

Include from context: without HTML escaping:

{{{examplevariable}}}

Show if has data:

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

Iterate over list:

{{#listdata}} 
    //some HTML shown for every list item
{{/listdata}}
Clone this wiki locally