Templates API

tpl.remove()

async

Removes template elements.

  • Accepts the template name, as a string, as the first argument.
    Accepts an optional second argument, which can be one of the following:
    Type
    Description
    object
    The template elements whose values match those specified in the object (case-insensitive) or match a regular expression defined in the object will be removed.
    array
    The template elements whose indexes match the specified numbers from the array will be removed.
    number
    The template element whose index corresponds to the specified number will be removed.
  • Returns an object with the following properties:
    Property
    Type
    Description
    it
    DOM element
    The block element where the template elements are placed.
    fields
    array
    An array of objects that remain after removing template elements with the following properties:
    Property
    Type
    Description
    it
    DOM element
    The template element.
    data
    object
    The data of the current template element.
    Returns null if no blocks with template elements are found.
  • It is not necessary to wait for the method to be executed using await if not needed.
Example:
await tpl.remove();
// false

await tpl.remove('tplDoesNotExist');
// null

// Removes all template elements.
await tpl.remove('tplName');
/*
{
   it: div[data-tpl="tplName"],
   fields: []
}
*/

// Removes the template element at index 0.
await tpl.remove('tplName', 0);
/*
{
   it: div[data-tpl="tplName"],
   fields: [
      {
         it: div.field,
         data: {name: 'book 2', secondName: 'cat 2'}
      }
   ]
}
*/

// Removes the template elements at indexes 0 and 1.
await tpl.remove('tplName', [0, 1]);
/*
{
   it: div[data-tpl="tplName"],
   fields: []
}
*/

/*
   Removes the template elements where the 'name' property is "book" or "book 2", 
   and the 'secondName' property matches the specified regular expression.
*/
await tpl.remove('tplName', {name: ['book', 'book 2'], secondName: /2/});
/*
{
   it: div[data-tpl="tplName"],
   fields: [
      {
         it: div.field,
         data: {name: 'book', secondName: 'cat'}
      }
   ]
}
*/