Templates API

tpl.swap()

async

Swaps the positions of two template elements.

  • Accepts the template name, as a string, as the first argument.
    Accepts second and third arguments, which can be one of the following:
    Type
    Description
    object
    The template element whose values match the specified ones in the object (case-insensitive), or whose values match the specified regular expression in the object will be selected.
    array
    The template element with the index corresponding to one of the specified numbers from the array will be selected.
    number
    The template element whose index corresponds to the specified number will be selected.
  • Returns an array of objects, each of which has the following properties:
    Property
    Type
    Description
    it
    DOM element
    The template element.
    data
    object
    The data of the current template element.
    The order of objects in the array will correspond to the selected template elements.
    Returns null if at least one template element is not selected or if it is not possible to swap their positions.
  • It is not necessary to wait for the method to be executed using await if not needed.
Example:
await tpl.swap();
// false

await tpl.swap('tplName:second', 0, 1);
// null

await tpl.swap('tplName', 0, 1);
await tpl.swap('tplName', [0, 1], 1);
await tpl.swap('tplName', {name: 'book'}, {name: 'book 2'});
/*
[
   {
      it: div.field,
      data: {name: 'book', secondName: 'cat'}
   },
   {
      it: div.field,
      data: {name: 'book 2', secondName: 'cat 2'}
   }
]
*/

await tpl.swap('tplName', {name: /2/, secondName: 'cat 2'}, {name: 'book'});
await tpl.swap('tplName', {name: ['book 2', 'book']}, 0);
/*
[
   {
      it: div.field,
      data: {name: 'book 2', secondName: 'cat 2'}
   },
   {
      it: div.field,
      data: {name: 'book', secondName: 'cat'}
   }
]
*/