Templates API
tpl.get()
asyncGets data of template elements.
-
Accepts the template name as a string, which is the first optional argument.
Accepts an optional second argument, which can be one of the following:TypeDescriptionobjectThe "fields" property will contain only template elements whose values match the specified ones in the object (case-insensitive), or whose values match the specified regular expression in the object.arrayThe "fields" property will contain only template elements with indexes corresponding to the specified numbers from the array.numberThe "fields" property will contain only the template element whose index corresponds to the specified number. -
Returns an object with the following properties if a template name is passed:
PropertyTypeDescriptionitDOM elementThe block element where the template elements are placed.fieldsarrayAn array of objects with the following properties:PropertyTypeDescriptionitDOM elementThe template element.dataobjectThe data of the current template element.
Returns null if no blocks with template elements are found.
Example:
await tpl.get();
/*
{
tplName: {
it: div[data-tpl="tplName"],
fields: [
{
it: div.field,
data: {name: 'book', secondName: 'cat'}
},
{
it: div.field,
data: {name: 'book 2', secondName: 'cat 2'}
}
]
},
'tplName:second': {
it: div[data-tpl="tplName:second"],
fields: []
}
}
*/
await tpl.get('tplName');
await tpl.get('tplName', [0, 1]);
await tpl.get('tplName', {name: ['book', 'book 2']});
/*
{
it: div[data-tpl="tplName"],
fields: [
{
it: div.field,
data: {name: 'book', secondName: 'cat'}
},
{
it: div.field,
data: {name: 'book 2', secondName: 'cat 2'}
}
]
}
*/
await tpl.get('tplName', 0);
await tpl.get('tplName', {name: 'book'});
/*
{
it: div[data-tpl="tplName"],
fields: [
{
it: div.field,
data: {name: 'book', secondName: 'cat'}
}
]
}
*/
await tpl.get('tplName', {name: /2/, secondName: 'cat 2'});
/*
{
it: div[data-tpl="tplName"],
fields: [
{
it: div.field,
data: {name: 'book 2', secondName: 'cat 2'}
}
]
}
*/
await tpl.get('tplName:second');
/*
{
it: div[data-tpl="tplName:second"],
fields: []
}
*/
await tpl.get('tplDoesNotExist');
// null