Templates API

tpl.add()

async

Adds new template elements.

  • 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.
    Accepts an optional last argument, which can be one of the following:
    Argument
    Description
    number
    Places the template element at the position specified by the index.
    'begin'
    Places the template element first.
    'end'
    Places the template element last.
    This is the default value.
  • Returns an object with the following properties:
    Property
    Type
    Description
    it
    DOM element
    The added template element.
    data
    object
    The data of the added template element.
    Returns an array of objects if multiple template elements were added.
    Returns null if adding template elements failed.
  • It is not necessary to wait for the method to be executed using await if not needed.
Example:
await tpl.add();
// false

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

// Adds a single empty template element.
await tpl.add('tplName');
await tpl.add('tplName', {});
/*
{
   it: div.field,
   data: null
}
*/

// Adds three empty template elements.
await tpl.add('tplName', {}, {}, {});
/*
[
   {
      it: div.field,
      data: null
   },
   {
      it: div.field,
      data: null
   },
   {
      it: div.field,
      data: null
   }
]
*/

// Adds a single filled template element to the second position, at index 1.
await tpl.add('tplName', {name: 'book', secondName: 'cat'}, 1);
/*
{
   it: div.field,
   data: {name: 'book', secondName: 'cat'}
}
*/

// Adds two filled template elements to the first position.
await tpl.add('tplName', {name: 'book', secondName: 'cat'}, {name: 'book 2', secondName: 'cat 2'}, 'begin');
/*
[
   {
      it: div.field,
      data: {name: 'book', secondName: 'cat'}
   },
   {
      it: div.field,
      data: {name: 'book 2', secondName: 'cat 2'}
   }
]
*/