![Logo](/_astro/logo.DNC1PCTe_Z1abT04.webp)
Enter a search term above to see results...
Enter a search term above to see results...
Semantic templates compile to an AST that can be read by a rendering engine to generate the html for your UI component.
The renderer handles things like dynamic expression evaluation, reactive DOM updates, and all the gnitty gritty to render your component to a page.
Semantic ships with a single primary rendering engine written to wrap Lit’s powerful and efficient rendering called LitRenderer
.
Additional rendering engines may be written in the future to render UI components to other targets than just web.
Advanced Use - It is not recommended to use the LitRenderer class directly unless you are wrapping SUI for a custom implementation.
LitRenderer
is used automatically fromdefineComponent
to render your component.
import { LitRenderer } from '@semantic-ui/renderer';
new LitRenderer(options)
Name | Type | Required | Description |
---|---|---|---|
options | Object | Yes | Configuration options for the renderer |
Property | Type | Required | Description |
---|---|---|---|
ast | Array | Yes | Abstract syntax tree of the template |
data | Object | No | Initial data context |
template | Template | Yes | Parent template instance |
subTemplates | Object | No | Map of available subtemplates |
snippets | Object | No | Map of template snippets |
helpers | Object | No | Map of template helper functions |
isSVG | boolean | No | Whether content should be rendered as SVG |
const renderer = new LitRenderer({ ast: templateAST, data: { user: { name: 'John' } }, template: parentTemplate, subTemplates: { header: headerTemplate, footer: footerTemplate }});
render({ ast, data } = {})
Renders the template with the current data context and returns a Lit template result.
Name | Type | Required | Description |
---|---|---|---|
options | Object | No | Render configuration options |
Property | Type | Default | Description |
---|---|---|---|
ast | Array | this.ast | Template AST to render |
data | Object | this.data | Data context for rendering |
A Lit template result.
// Basic renderconst html = renderer.render();
// Render with new dataconst html = renderer.render({ data: { count: 5 }});
// Render with new AST and dataconst html = renderer.render({ ast: newTemplateAST, data: { items: ['one', 'two'] }});
setData(newData)
Updates the data context and triggers reactive updates.
Name | Type | Required | Description |
---|---|---|---|
newData | Object | Yes | New data context |
renderer.setData({ count: 10, user: { name: 'Jane' }});
evaluateExpression(expression, data, options)
Evaluates a template expression in the given data context.
Name | Type | Required | Description |
---|---|---|---|
expression | string | Yes | Expression to evaluate |
data | Object | No | Data context for evaluation |
options | Object | No | Evaluation options |
Property | Type | Default | Description |
---|---|---|---|
asDirective | boolean | false | Return result as a Lit directive |
ifDefined | boolean | false | Use Lit’s ifDefined directive |
unsafeHTML | boolean | false | Use Lit’s unsafeHTML directive |
The evaluated expression result or a Lit directive if asDirective
is true.
// Simple property accessconst value = renderer.evaluateExpression('user.name', { user: { name: 'John' }});
// With directive optionsconst directive = renderer.evaluateExpression('content', data, { asDirective: true, unsafeHTML: true});
evaluateTemplate(node, data)
Evaluates a template node with given data.
Name | Type | Required | Description |
---|---|---|---|
node | Object | Yes | Template node to evaluate |
data | Object | No | Data context |
Property | Type | Required | Description |
---|---|---|---|
name | string | Yes | Template name to render |
data | Object | No | Template data context |
A Lit template result.
const result = renderer.evaluateTemplate({ name: 'itemTemplate', data: { item: { id: 1, text: 'Test' } }}, contextData);
evaluateConditional(node, data)
Evaluates a conditional template node.
Name | Type | Required | Description |
---|---|---|---|
node | Object | Yes | Conditional node to evaluate |
data | Object | No | Data context |
Property | Type | Required | Description |
---|---|---|---|
condition | string | Yes | Condition to evaluate |
content | Array | Yes | Content if true |
branches | Array | No | Else/elseif branches |
A Lit directive for conditional rendering.
const result = renderer.evaluateConditional({ condition: 'isVisible', content: [/* template content */], branches: [ { type: 'elseif', condition: 'isLoading', content: [/* loading content */] }, { type: 'else', content: [/* else content */] } ]}, data);
evaluateEach(node, data)
Evaluates an iteration template node.
Name | Type | Required | Description |
---|---|---|---|
node | Object | Yes | Each node to evaluate |
data | Object | No | Data context |
Property | Type | Required | Description |
---|---|---|---|
over | string | Yes | Array to iterate over |
content | Array | Yes | Template for each item |
as | string | No | Item alias name |
A Lit directive for iteration.
const result = renderer.evaluateEach({ over: 'items', content: [/* item template */], as: 'item'}, { items: ['one', 'two', 'three']});
The renderer supports various expression types in templates, for more information see the documentation on template expression.
{ user.name }
{ helper (otherHelper value) }
<div hidden={ isHidden }></div>
{ formatDate date "MM/DD/YYYY" }