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
- Source:
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
Throws:
-
when unknown compiler name provided.
- Type
- Error
- Source:
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
- Source:
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
Throws:
-
when unknown compiler provided
- Type
- Error
- Source:
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:
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:
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
- Source: