Templates API

tpl.pos()

async

Computes the index of the template element, whether the passed element is the template element itself or one of its descendants, along with the data of the template containing this element.

  • Accepts a single argument as a DOM element.
  • Returns an object with the following properties:
    Property
    Type
    Description
    tpl
    DOM element
    A block containing template elements.
    name
    string
    The name of the template in which the passed element is placed.
    field
    DOM element
    The template element itself, even if a descendant is passed.
    idx
    number
    The index of the template element itself, even if a descendant is passed.
    Returns null if the passed argument is not a DOM element or is not inside a template block.
Example:
await tpl.pos();
// false

await tpl.pos(null);
await tpl.pos($('body'));
// null

await tpl.pos($('[data-tpl="tplName"] .book'));
/*
{
   tpl: div[data-tpl="tplName"],
   name: 'tplName',
   field: div.field,
   idx: 0
}
*/

await tpl.pos($('[data-tpl="tplName"] > * ? 1'));
/*
{
   tpl: div[data-tpl="tplName"],
   name: 'tplName',
   field: div.field,
   idx: 1
}
*/