On This Page
Template
The Template
class represents a compiled template and provides methods for manipulating and rendering it. It integrates with the Semantic UI component system and supports various rendering engines.
SUI is designed to be flexible in terms of how components are rendered, currently Semantic ships with the Lit
rendering engine which will compile templates to lit html
for use with web components.
Advanced Use Only - When using Semantic UI components, a template will automatically be created from
defineComponent
. It is not recommended to useTemplate
class on its own except for advanced use-cases.
Constructor
Syntax
Parameters
Name | Type | Description |
---|---|---|
options | Object | Configuration options for the template |
Options
Name | Type | Description |
---|---|---|
templateName | string | Name of the template |
ast | Object[] | Compiled AST of the template |
template | string | The template string |
data | Object | Initial data for the template |
element | Element | The DOM element associated with the template |
renderRoot | Element | The root element for rendering |
css | string | CSS styles associated with the template |
events | Object | Event handlers for the template |
keys | Object | Key bindings for the template |
stateConfig | Object | Configuration for reactive state |
subTemplates | Object | Sub-templates used within this template |
createComponent | Function | Function to create the component instance |
parentTemplate | Template | The parent template when nested |
renderingEngine | string | The rendering engine to use (default: ‘lit’) |
isPrototype | boolean | Whether this is a prototype template |
attachStyles | boolean | Whether to attach styles to the renderRoot |
onCreated | Function | Callback when the template is created |
onRendered | Function | Callback when the template is rendered |
onUpdated | Function | Callback when the template is updated |
onDestroyed | Function | Callback when the template is destroyed |
onThemeChanged | Function | Callback when the theme changes |
Usage Examples
Basic Initialization with Template String
Initialization with AST
Advanced Initialization
Methods
initialize
Initializes the template, setting up reactive state and event handlers.
Syntax
Usage
render
Renders the template with the current data.
Syntax
Parameters
Name | Type | Description |
---|---|---|
additionalData | Object | Additional data to merge for rendering |
Returns
TemplateResult
- The rendered template content (typically a Lit TemplateResult).
Usage
setDataContext
Updates the data context of the template.
Syntax
Parameters
Name | Type | Description |
---|---|---|
data | Object | New data to set |
options | Object | Additional options |
Usage
clone
Creates a copy of the template with optional new settings.
Syntax
Parameters
Name | Type | Description |
---|---|---|
settings | Object | New settings to apply to the clone |
Returns
Template
- A new Template instance.
Usage
attachEvents
Attaches event handlers to the rendered template.
Syntax
Parameters
Name | Type | Description |
---|---|---|
events | Object | Event handlers to attach |
Usage
Lifecycle Hooks
The Template class provides several lifecycle hooks that can be overridden to add custom behavior:
onCreated
onRendered
onDestroyed
onThemeChanged