Class: Template

jrt.Template(templatenon-null, targetopt, non-null)

This class lets you clone TEMPLATE elements and add those clones to the template's target element, which can be either the template element's parent or an element specified in the constructor.

Once a clone has been created with clone, it can be added to the template's target with prepend or append. Use clear to remove from the template's target all previously-added elements.

Constructor

new Template(templatenon-null, targetopt, non-null)

Creates a template helper for the specified TEMPLATE element and target element.

If 'target' is not specified, then the TEMPLATE element's parent becomes the target and the cloned elements are appended or prepended at the point where the TEMPLATE element is located in the document. In this case the template element is removed from the DOM, so this class can only be instatiated once for a given template.

If 'target' is specified, then it will be filled with the template clones. In this case the template element is not removed from the document.

Parameters:
Name Type Attributes Description
template HTMLTemplateElement

The template to be cloned and inserted in the document.

target HTMLElement <optional>

The element to be filled with the template. Defaults to the template's parent.

Throws:

If the target element is not empty.

Type
jrt.JrtError

Members

(non-null) target :HTMLElement

Type:
  • HTMLElement

(non-null) template :HTMLTemplateElement

Type:
  • HTMLTemplateElement

Methods

append(elem) → {HTMLElement|DocumentFragment}

Inserts the last element created with clone after the last of all elements previously inserted.

Parameters:
Name Type Description
elem HTMLElement | DocumentFragment

Use this to insert a specific element, instead of the last clone.

Throws:

If no clone has been previously created.

Type
jrt.JrtError
Returns:

The inserted element or document fragment.

Type
HTMLElement | DocumentFragment

clear()

Removes all elements inserted with either prepend or append.

clone() → {HTMLElement|DocumentFragment}

Creates a new copy of the template that you can customise and add to the document with prepend or append.

Returns:

If the template just contains one element node (apart from whitespace and comment nodes), then this method will return an element. Otherwise, it will return a document fragment.

Type
HTMLElement | DocumentFragment

prepend(elem) → {HTMLElement|DocumentFragment}

Inserts the last element created with clone before the first of all elements previously inserted.

Parameters:
Name Type Description
elem HTMLElement | DocumentFragment

Use this to insert a specific element, instead of the last clone.

Throws:

If no clone has been previously created.

Type
jrt.JrtError
Returns:

The inserted element or document fragment.

Type
HTMLElement | DocumentFragment