Templates API
tpl.replace()
asyncReplaces all the current template elements with new ones.
-
Accepts the template name, as a string, as the first argument.
Accepts any number of optional arguments, starting from the second, in the form of objects containing data for new template elements. -
Returns an object with the following properties:
PropertyTypeDescriptionitDOM elementThe new template element.dataobjectThe data of the new template element.
Returns null if replacement template elements failed. - It is not necessary to wait for the method to be executed using await if not needed.
Example:
await tpl.replace();
// false
await tpl.replace('tplDoesNotExist');
// null
// Replaces all template elements with one empty one.
await tpl.replace('tplName');
await tpl.replace('tplName', {});
/*
{
it: div.field,
data: null
}
*/
// Replaces all template elements with three empty ones.
await tpl.replace('tplName', {}, {}, {});
/*
[
{
it: div.field,
data: null
},
{
it: div.field,
data: null
},
{
it: div.field,
data: null
}
]
*/
// Replaces all template elements with a single filled one.
await tpl.replace('tplName', {name: 'book', secondName: 'cat'});
/*
{
it: div.field,
data: {name: 'book', secondName: 'cat'}
}
*/
// Replaces all template elements with two filled ones.
await tpl.replace('tplName', {name: 'book', secondName: 'cat'}, {name: 'book 2', secondName: 'cat 2'});
/*
[
{
it: div.field,
data: {name: 'book', secondName: 'cat'}
},
{
it: div.field,
data: {name: 'book 2', secondName: 'cat 2'}
}
]
*/