Manual:HTML templates

Starting with , MediaWiki can generate HTML content from Mustache templates on the server and on the client.

Server-side template parsing is implemented in PHP via the TemplateParser class, which acts as a wrapper around the lightncandy library.

Client-side templates are supported via   ResourceLoader modules and the   library.

Creating templates
To use HTML templates in your code, first create a Mustache template file with a   file extension, for example,  .

Templates should contain as little programming logic as possible so that they are easily readable and provide proper separation of concerns.

If your template is part of the core MediaWiki software, put it in core's <tvar|1> </> directory.

If it is part of an extension, you should create a dedicated <tvar|1> </> directory within your extension's directory to hold it.

Templates should follow the [<tvar|url>https://mustache.github.io/mustache.5.html</> Mustache-5 specification].

TemplateParser (server-side)
This class finds template files, reads them, compiles them into PHP code, and expands tags in the template using data you provide to produce the output HTML.

MediaWiki compiles templates as needed, and uses caching to store the compiled templates if available (see 1>#Caching</>|#Caching below).

This avoids developers having to compile templates into PHP files during development or as a build step, or the server writing them to the file system during operation.

To use <tvar|1> </>, first create a new instance of the class:

If your Mustache templates do not reside in core's <tvar|1> </>, you need to pass the path to where they reside as the first parameter to the constructor (either relative to your MediaWiki root or relative to the calling file's directory with <tvar|2>__DIR__</>):

You then parse templates into HTML by invoking the <tvar|1> </> function.

The first parameter to this is the name of your template (the part of the filename before <tvar|1> </>).

The second parameter is an array providing the values needed by the Mustache tags in your template.

For example,

This replaces any instances of <tvar|1> </> and <tvar|2> </> tags in the "MyWidget" mustache template with the values you provided, and returns the resulting HTML.

(The <tvar|1> </> simply prints the generated HTML in the current output context.)

As an example of HTML templating, see <tvar|1></> as used by <tvar|2></>.

Caching
<tvar|1>TemplateParser</> attempts to cache the compiled PHP template.

It prefers to use <tvar|1> </> (See <tvar|2></>), but falls back to <tvar|3> </> (a general object cache like <tvar|4>Memcached</> or <tvar|5>Redis</>, see <tvar|6></>).

Partials
The cache is keyed on a hash of the template file's contents, so if you change a template file, the compiled template will update (you may need to clear your wiki's front-end cache using <tvar|1> </>).

However, this does not notice changes to [<tvar|url>https://mustache.github.io/mustache.5.html#Partials</> "partial" templates] that you include with  (bug <tvar|1>T113095</>).

So if you change a partial, you need to make cosmetic changes to the parent templates that include it, or restart your cache.

<tvar|1>mw.template</> (client-side)
To use a Mustache template on the client-side, add it to your <tvar|1></> module definition first:

The template definition above consists of two pieces, a file path (<tvar|1> </>) and an optional alias (<tvar|2> </>).

The alias must be suffixed with the name of the templating language (e.g. '<tvar|1> </>') so that it knows which compiler to use.

ResourceLoader automatically serves the supporting  JavaScript modules, so you don't need to mention anything in <tvar|2> </>.

Once you have added the template to your module, you can retrieve it in JavaScript using <tvar|1> </>:

To render the template and data into HTML output, call the compiled template's <tvar|1> </> function:

Partials
Partials are also supported on the client-side.

See <tvar|1>https://mustache.github.io/mustache.5.html#Partials</> for more information.