Templates API

tpl.replace()

async

Replaces all the current template elements with new ones.

  • 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.
  • Returns an object with the following properties:
    Property
    Type
    Description
    it
    DOM element
    The new template element.
    data
    object
    The data of the new template element.
    Returns an array of objects if multiple new template elements are created.
    Returns null if replacement template elements failed.
  • It is not necessary to wait for the method to be executed using await if not needed.
Example:
await tpl.replace();
// false

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

// Replaces all template elements with one empty one.
await tpl.replace('tplName');
await tpl.replace('tplName', {});
/*
{
   it: div.field,
   data: null
}
*/

// Replaces all template elements with three empty ones.
await tpl.replace('tplName', {}, {}, {});
/*
[
   {
      it: div.field,
      data: null
   },
   {
      it: div.field,
      data: null
   },
   {
      it: div.field,
      data: null
   }
]
*/

// Replaces all template elements with a single filled one.
await tpl.replace('tplName', {name: 'book', secondName: 'cat'});
/*
{
   it: div.field,
   data: {name: 'book', secondName: 'cat'}
}
*/

// Replaces all template elements with two filled ones.
await tpl.replace('tplName', {name: 'book', secondName: 'cat'}, {name: 'book 2', secondName: 'cat 2'});
/*
[
   {
      it: div.field,
      data: {name: 'book', secondName: 'cat'}
   },
   {
      it: div.field,
      data: {name: 'book 2', secondName: 'cat 2'}
   }
]
*/