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
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:
Returns:
Compiled template
- Type
- TemplateRenderer
Throws:
-
when unknown compiler name provided.
- Type
- Error
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
getCompiler(name) → {TemplateCompiler}static
#
Get a compiler via its name.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | Name of a compiler |
- Source:
Returns:
The compiler
- Type
- TemplateCompiler
Throws:
-
when unknown compiler provided
- Type
- Error
getCompilerName(templateName) → {string}static
#
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:
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