Manual:HTML templates

Starting with MediaWiki 1.25, MediaWiki can generate HTML content from Mustache templates. Server-side template parsing is implemented in PHP via the TemplateParser class, which acts as a wrapper around the lightncandy library. , support for client-side template parsing is pending for core, although you can use the Mantle extension.

Note: This is unrelated to the use of MediaWiki templates in wikitext (such as the MW file in this article). Templating here builds the HTML of a web page, not the wiki content within it.

Creating templates
To use HTML templates in your code, first create a Mustache template file with a .mustache file extension, for example, MyWidget.mustache. 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  directory. If it is part of an extension, you should create a dedicated templates directory within your extension's directory to hold it. Templates should follow the 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 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, first create a new instance of the class:

If your Mustache templates do not reside in core's includes/templates, you need to pass the path to where they reside as the first parameter to the constructor (relative to your MediaWiki root):

You then parse templates into HTML by invoking the  function. The second parameter to this is an array providing the values needed by the Mustache tags in your template. For example,

This replaces any instances of  and   tags in the "MyWidget" mustache template with the values you provided, and returns the resulting HTML. (The  simply prints the generated HTML in the current output context.)

As an example of HTML templating, see as used by.

Caching
TemplateParser attempts to caches the compiled PHP template. It first tries to use, then falls back to. The former is PHP's cache used mostly for opcode caching, the latter is whatever general-purpose cache you have installed, e.g. memcached or redis. See Manual:Cache.
 * (CACHE_ACCEL is not available on a simple Zend PHP 5.5 install,  reports  CACHE_ACCEL requested but no suitable object cache is present. You may want to install APC.  but APC is obsolete in PHP 5.5.  Is CACHE_ACCEL available in WMF production? Is it on HHVM?) -- SPage (WMF) (talk) 03:33, 21 February 2015 (UTC)