mw.template

An extensible library for rendering templates in different template languages. By default only the html template library is provided. The Mustache library is also provided in mediawiki core via the mediawiki.template.mustache library.

Example

// returns $( '<div>hello world</div>' );
const $node = mw.template.compile( '<div>hello world</div>', 'html' ).render();

// also returns $( '<div>hello world</div>' );
mw.loader.using( 'mediawiki.template.mustache' ).then( () => {
  const $node = mw.template.compile( '<div>{{ >Foo }}</div>', 'mustache' ).render( {
    text: 'Hello world'
  }, {
    Foo: mw.template.compile( '{{text}}', 'mustache' )
  } );
} );

Methods

add(moduleName, templateName, templateBody) → {TemplateRenderer}static #

Register a template associated with a module.

Precompiles the newly added template based on the suffix in its name.

Parameters:

Name Type Description
moduleName string

Name of the ResourceLoader module the template is associated with

templateName string

Name of the template (including suffix)

templateBody string

Contents of the template (e.g. html markup)

Source:

Returns:

Compiled template

Type
TemplateRenderer
Register a template associated with a module.

compile(templateBody, compilerName) → {TemplateRenderer}static #

Compile a string of template markup with an engine of choice.

Parameters:

Name Type Description
templateBody string

Template body

compilerName string

The name of a registered compiler.

Source:

Throws:

when unknown compiler name provided.

Type
Error

Returns:

Compiled template

Type
TemplateRenderer
Compile a string of template markup with an engine of choice.

get(moduleName, templateName) → {TemplateRenderer}static #

Get a compiled template by module and template name.

Parameters:

Name Type Description
moduleName string

Name of the module to retrieve the template from

templateName string

Name of template to be retrieved

Source:

Returns:

Compiled template

Type
TemplateRenderer
Get a compiled template by module and template name.

getCompiler(name) → {TemplateCompiler}static #

Get a compiler via its name.

Parameters:

Name Type Description
name string

Name of a compiler

Source:

Throws:

when unknown compiler provided

Type
Error

Returns:

The compiler

Type
TemplateCompiler
Get a compiler via its name.

getCompilerName(templateName) → {string}static #

Get the name of the associated compiler based on a template name.

Parameters:

Name Type Description
templateName string

Name of a template (including suffix)

Source:

Returns:

Name of a compiler

Type
string
Get the name of the associated compiler based on a template name.

registerCompiler(name, compiler)static #

Register a new compiler.

A compiler is any object that implements a mw.template.compile method. The compile() method must return a Template interface with a method render() that returns HTML.

The compiler name must correspond with the name suffix of templates that use this compiler.

Parameters:

Name Type Description
name string

Compiler name

compiler TemplateCompiler
Source:
Register a new compiler.

Type Definitions

TemplateCompileFunction(src) → {TemplateRenderer} #

Compiles a template for rendering.

Parameters:

Name Type Description
src string

source of the template

Source:

Returns:

for rendering

Type
TemplateRenderer
Compiles a template for rendering.

TemplateCompiler #

Type:

Properties:

Name Type Description
compile TemplateCompileFunction
Source:

TemplateRenderFunction(data, partials) → {jQuery} #

Renders the template to create a jQuery object.

Parameters:

Name Type Attributes Description
data Object optional

for the template

partials Object optional

additional partial templates

Source:

Returns:

Type
jQuery
Renders the template to create a jQuery object.

TemplateRenderer #

Type:

Properties:

Name Type Description
render TemplateRenderFunction
Source: