Templates API
tpl.set()
asyncSets new values for the template elements.
-
Accepts the template name, as a string, as the first argument.
Accepts a second argument as an object containing data to set.
Accepts an optional third argument, which can be one of the following:TypeDescriptionobjectNew values will be set for template elements whose values match those specified in the object (case-insensitive) or match the regular expression specified in the object.arrayNew values will be set for template elements with indexes matching one of the specified numbers in the array.numberNew values will be set for the template element whose index corresponds to the specified number. -
Returns an array of objects, each of which has the following properties:
PropertyTypeDescriptionitDOM elementThe template element in which new values were set.dataobjectAll data of the template element in which new values were set.
Returns null if no blocks with template elements are found or if no values are passed to be set. - It is not necessary to wait for the method to be executed using await if not needed.
Example:
await tpl.set();
// false
await tpl.set('tplName');
// null
// Sets the passed data in the template element at index 99.
await tpl.set('tplName', {name: 'book 3', secondName: 'cat 3'}, 99);
// []
// Sets the passed data in all of the template elements.
await tpl.set('tplName', {name: 'book 3'});
/*
[
{
it: div.field,
data: {name: 'book 3', secondName: 'cat'}
},
{
it: div.field,
data: {name: 'book 3', secondName: 'cat 2'}
}
]
*/
// Sets the passed data in the template element at index 0.
await tpl.set('tplName', {name: 'book 3', secondName: 'cat 3'}, 0);
/*
[
{
it: div.field,
data: {name: 'book 3', secondName: 'cat 3'}
}
]
*/
// Sets the passed data in the template elements at indexes 0 and 1.
await tpl.set('tplName', {name: 'book 3'}, [0, 1]);
/*
[
{
it: div.field,
data: {name: 'book 3', secondName: 'cat'}
},
{
it: div.field,
data: {name: 'book 3', secondName: 'cat 2'}
}
]
*/
// Sets the passed data to template elements with a 'name' property equal to "book".
await tpl.set('tplName', {name: 'book 3'}, {name: 'book'});
/*
[
{
it: div.field,
data: {name: 'book 3', secondName: 'cat'}
}
]
*/
/*
Sets the passed data to template elements where the 'name' property is either "book" or "book 2"
and the 'secondName' property matches the specified regular expression.
*/
await tpl.set('tplName', {name: 'book 3', secondName: 'cat 3'}, {name: ['book', 'book 2'], secondName: /2/});
/*
[
{
it: div.field,
data: {name: 'book 3', secondName: 'cat 3'}
}
]
*/