Class Template<T extends Template<T>>

All Implemented Interfaces:
HeadElement

public class Template<T extends Template<T>> extends StyledContainerElement<T> implements HeadElement
Represents an HTML <template> element for declaring inert HTML fragments.

The <template> element holds HTML content that is not rendered when the page loads but can be instantiated later using JavaScript. It is used for client-side templating, web components, and dynamic content generation. The content inside a template is parsed but not displayed, and scripts within it do not execute until the template is cloned and inserted into the document.

Features:

  • Holds inert HTML content not rendered on page load
  • Content can be cloned and inserted via JavaScript
  • Essential for web components and shadow DOM
  • Scripts and styles inside are not processed until instantiated
  • Supports optional shadow DOM mode via shadowrootmode attribute

Usage:


 // Create a template for a card component
 Template cardTemplate = new Template();
 cardTemplate.setId("card-template");
 Div card = new Div();
 card.addClass("card");
 card.addElement(new H3("Title"));
 card.addElement(new P("Content"));
 cardTemplate.addElement(card);

 // Declarative shadow DOM
 Template shadow = new Template();
 shadow.setShadowRootMode("open");
 
Since:
2026
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • invalid reference
    StyledElement
  • Slot
  • Constructor Details

    • Template

      public Template()
      Constructs an empty <template> element.
  • Method Details

    • setShadowRootMode

      public final T setShadowRootMode(String mode)
      Sets the shadow root mode for declarative shadow DOM.

      When set, the template creates a shadow root on its parent element. Valid values are "open" and "closed".

      Parameters:
      mode - the shadow root mode ("open" or "closed")
      Returns:
      this element for method chaining
    • setShadowRootClonable

      public final T setShadowRootClonable(boolean shadowRootClonable)
      Sets the shadowrootclonable boolean attribute.

      Indicates that the declarative shadow root should be clonable.

      Parameters:
      shadowRootClonable - True to make the shadow root clonable, false to remove.
      Returns:
      This element for method chaining.
    • setShadowRootDelegatesFocus

      public final T setShadowRootDelegatesFocus(boolean shadowRootDelegatesFocus)
      Sets the shadowrootdelegatesfocus boolean attribute.

      Indicates that the declarative shadow root should delegate focus to the first focusable element.

      Parameters:
      shadowRootDelegatesFocus - True to delegate focus, false to remove.
      Returns:
      This element for method chaining.
    • setShadowRootSerializable

      public final T setShadowRootSerializable(boolean shadowRootSerializable)
      Sets the shadowrootserializable boolean attribute.

      Indicates that the declarative shadow root can be serialized.

      Parameters:
      shadowRootSerializable - True to make serializable, false to remove.
      Returns:
      This element for method chaining.