Expand all

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) → {mw.template~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)

Returns:

Compiled template

Type
mw.template~TemplateRenderer
Source:
Register a template associated with a module.

compile(templateBody, compilerName) → {mw.template~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.

Returns:

Compiled template

Type
mw.template~TemplateRenderer

Throws:

when unknown compiler name provided.

Type
Error
Source:
Compile a string of template markup with an engine of choice.

get(moduleName, templateName) → {mw.template~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

Returns:

Compiled template

Type
mw.template~TemplateRenderer
Source:
Get a compiled template by module and template name.

getCompiler(name) → {mw.template~TemplateCompiler}static #

Get a compiler via its name.

Parameters:

Name Type Description
name string

Name of a compiler

Returns:

The compiler

Type
mw.template~TemplateCompiler

Throws:

when unknown compiler provided

Type
Error
Source:
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)

Returns:

Name of a compiler

Type
string
Source:
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 mw.template~TemplateCompiler
Source:
Register a new compiler.

Type Definitions

TemplateCompileFunction(src) → {mw.template~TemplateRenderer} #

Compiles a template for rendering.

Parameters:

Name Type Description
src string

source of the template

Returns:

for rendering

Type
mw.template~TemplateRenderer
Source:
Compiles a template for rendering.

TemplateCompiler #

Type:

Properties:

Name Type Description
compile mw.template~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

Returns:

Type
jQuery
Source:
Renders the template to create a jQuery object.

TemplateRenderer #

Type:

Properties:

Name Type Description
render mw.template~TemplateRenderFunction
Source: