
Enter a search term above to see results...
Enter a search term above to see results...
The TemplateCompiler
class is responsible for compiling template strings into an Abstract Syntax Tree (AST). This precompilation step enables efficient rendering and updates, especially useful for server-side rendering (SSR) and advanced use cases.
new TemplateCompiler(templateString)
Name | Type | Description |
---|---|---|
templateString | string | The template string to compile |
import { TemplateCompiler } from '@semantic-ui/templating';
const compiler = new TemplateCompiler('<div>{{name}}</div>');
Compiles the template string into an Abstract Syntax Tree (AST).
compiler.compile()
Object
- The compiled [AST]](/api/templating/ast) representation of the template.
const ast = compiler.compile();console.log(ast);
Parses a template string, typically used for partial templates or snippets.
TemplateCompiler.parseTemplateString(expression)
Name | Type | Description |
---|---|---|
expression | string | The template string to parse |
Object
- An object containing parsed template information.
const templateInfo = TemplateCompiler.parseTemplateString('{{> partialName data1=value1 data2=value2}}');console.log(templateInfo);
Detects whether the template uses single {}
or double {{}}
bracket syntax.
TemplateCompiler.detectSyntax(templateString)
Name | Type | Description |
---|---|---|
templateString | string | The template string to analyze |
string
- Either 'singleBracket'
or 'doubleBracket'
.
const syntax = TemplateCompiler.detectSyntax('{{name}}');console.log(syntax); // 'doubleBracket'
Preprocesses the template string, handling special cases like self-closing web component tags.
TemplateCompiler.preprocessTemplate(templateString)
Name | Type | Description |
---|---|---|
templateString | string | The template string to preprocess |
string
- The preprocessed template string.
const processed = TemplateCompiler.preprocessTemplate('<ui-icon icon="foo" />');console.log(processed); // '<ui-icon icon="foo"></ui-icon>'
Optimizes the compiled AST by joining neighboring HTML nodes.
TemplateCompiler.optimizeAST(ast)
Name | Type | Description |
---|---|---|
ast | Object[] | The AST to optimize |
Object[]
- The optimized AST.
const optimizedAST = TemplateCompiler.optimizeAST(ast);
You can precompile your templates on the serve then load them on the client using modern build tooling like esbuild or rollup and JSON imports.
On the server we want to render our templates to an AST.
import { TemplateCompiler } from '@semantic-ui/templating';import fs from 'fs/promises';import path from 'path';import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function precompileTemplates(templateName) { const templatePath = path.join(__dirname, 'templates', `${templateName}.html`); const outputPath = path.join(__dirname, 'ast', `${templateName}.json`);
const template = await fs.readFile(templatePath, 'utf-8'); const compiler = new TemplateCompiler(template); const ast = compiler.compile();
await fs.writeFile(outputPath, JSON.stringify(ast, null, 2));}
// somewhere elseprecompileTemplates('user-profile');
Then on the client we can import that files directly as JSON
file directly and pass as the ast
parameter in defineComponent
.
import { defineComponent } from '@semantic-ui/component';import ast from './ast/user-profile.json' assert { type: 'json' };
export function defineUserProfile() { defineComponent({ tagName: 'user-profile', ast, // rest of component });}