vael:template
Go text/template engine with Sprig helpers, template sets, and HTML escaping.
Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes.
Supports one-shot rendering, pre-compiled Template objects, multi-template
TemplateSet layouts (define/block/template), bounded execution
limits, and optional automatic HTML escaping.
import ... from "vael:template"
vael-template (enabled by default in vaeljs-core and vaeljs-lite-core)Example
Section titled “Example”import { createTemplateSet, render } from "vael:template";
console.log(render("Hello, {{ upper .name }}!", { name: "vael" }));
const set = createTemplateSet({ htmlEscape: true }) .add("layout", "<html><body>{{ block \"content\" . }}{{ end }}</body></html>") .add("page", "{{ template \"layout\" . }}{{ define \"content\" }}<p>{{ .msg }}</p>{{ end }}");console.log(set.render("page", { msg: "<safe>" }));renderv0.0.0-alpha.0text/template once against optional JSON object data.compilev0.0.0-alpha.0text/template once for repeated rendering.createTemplateSetv0.0.0-alpha.0TemplateSet for composing layouts and partials.escapeHtmlv0.0.0-alpha.0&, <, >, ", ') in input.TemplateOptionsv0.0.0-alpha.0Templatev0.0.0-alpha.0text/template ready for repeated rendering.TemplateSetv0.0.0-alpha.0define, template, and block layouts and partials.Functions
Section titled “Functions”render
Section titled “render”export function render(
source: string,
data?: unknown,
options?: TemplateOptions,
): string;
Compile and render a Go text/template once against optional JSON object
data.
| Name | Type | Description |
|---|---|---|
source | string | Go text/template source text. |
data optional | unknown | Optional JSON object context (defaults to {}). |
options optional | TemplateOptions | Optional Sprig, HTML-escaping, and resource limit options. |
string — Rendered template output string.TypeError— on wrong argument types.Error— withcode: TemplateErrorCode.
import { render } from "vael:template";
console.log(render("Items: {{ len .items }}", { items: [1, 2, 3] }));compile
Section titled “compile”export function compile(
source: string,
options?: TemplateOptions,
): Template;
Compile a Go text/template once for repeated rendering.
| Name | Type | Description |
|---|---|---|
source | string | Go text/template source text. |
options optional | TemplateOptions | Optional Sprig, HTML-escaping, and resource limit options. |
Template — Pre-compiled Template handle.TypeError— on wrong argument types.Error— withcode: TemplateErrorCode.
createTemplateSet
Section titled “createTemplateSet”export function createTemplateSet(options?: TemplateOptions): TemplateSet;
Create an empty TemplateSet for composing layouts and partials.
| Name | Type | Description |
|---|---|---|
options optional | TemplateOptions | Optional Sprig, HTML-escaping, and resource limit options. |
TemplateSet — Empty TemplateSet instance.Error— withcode"INVALID_DATA"on invalid options.
escapeHtml
Section titled “escapeHtml”export function escapeHtml(input: string): string;
Escape HTML special characters (&, <, >, ", ') in input.
| Name | Type | Description |
|---|---|---|
input | string | Plain text string to HTML-escape. |
string — HTML-escaped string.Error— withcode"INVALID_DATA"ifinputis not a string.
import { escapeHtml } from "vael:template";
console.log(escapeHtml("<script>")); // "<script>"Interfaces
Section titled “Interfaces”TemplateOptions
Section titled “TemplateOptions”export interface TemplateOptions {
sprig?: boolean;
hermetic?: boolean;
htmlEscape?: boolean;
maxSourceLength?: number;
maxNestingDepth?: number;
maxOutputLength?: number;
maxIterations?: number;
maxFunctionCalls?: number;
}
Options controlling Sprig helpers, HTML auto-escaping, and safety bounds.
| Property | Type | Description | Since |
|---|---|---|---|
sprig optional | boolean | Enable safe deterministic Sprig template helpers (default: true). | v0.0.0-alpha.0 |
hermetic optional | boolean | Restrict Sprig helpers to hermetic functions, omitting clock- and environment-reading helpers such as now and ago (default: false). | v0.0.0-alpha.0 |
htmlEscape optional | boolean | Automatically HTML-escape interpolated values unless marked with safeHtml (default: false). | v0.0.0-alpha.0 |
maxSourceLength optional | number | Maximum template source length in UTF-8 bytes (default: 1_048_576). | v0.0.0-alpha.0 |
maxNestingDepth optional | number | Maximum control-structure nesting depth (default: 100). | v0.0.0-alpha.0 |
maxOutputLength optional | number | Maximum rendered output length in UTF-8 bytes (default: 4_194_304). | v0.0.0-alpha.0 |
maxIterations optional | number | Maximum range loop iterations per render (default: 100_000). | v0.0.0-alpha.0 |
maxFunctionCalls optional | number | Maximum helper function calls per render (default: 100_000). | v0.0.0-alpha.0 |
Template
Section titled “Template”export interface Template {
render(data?: unknown): string;
}
A pre-compiled Go text/template ready for repeated rendering.
Template.render
Section titled “Template.render”render(data?: unknown): string;
Render the compiled template against an optional JSON data object.
| Name | Type | Description |
|---|---|---|
data optional | unknown | Optional JSON object context (defaults to {}). |
string — Rendered template output string.Error— withcode: TemplateErrorCode.
import { compile } from "vael:template";
const tpl = compile("Hi {{ .user }}");console.log(tpl.render({ user: "Ada" }));TemplateSet
Section titled “TemplateSet”export interface TemplateSet {
add(name: string, source: string): TemplateSet;
render(name: string, data?: unknown): string;
names(): string[];
}
A named collection of templates supporting define, template, and
block layouts and partials.
TemplateSet.add
Section titled “TemplateSet.add”add(name: string, source: string): TemplateSet;
Validate and register a named template in the set, replacing any existing
entry with the same name. Returns this for chaining.
| Name | Type | Description |
|---|---|---|
name | string | Unique template name. |
source | string | Go text/template source text. |
TemplateSet — this for chaining.Error— withcode"INVALID_NAME","INVALID_DATA","LIMIT_EXCEEDED", or"PARSE".
TemplateSet.render
Section titled “TemplateSet.render”render(name: string, data?: unknown): string;
Render the registered template name against optional JSON object data.
| Name | Type | Description |
|---|---|---|
name | string | Registered template name to execute. |
data optional | unknown | Optional JSON object context (defaults to {}). |
string — Rendered template output string.Error— withcode"TEMPLATE_NOT_FOUND","INVALID_NAME","INVALID_DATA","LIMIT_EXCEEDED","PARSE", or"RENDER".
TemplateSet.names
Section titled “TemplateSet.names”names(): string[];
Return the registered template names in sorted order.
string[] — Lexicographically sorted list of registered template names.Type Aliases
Section titled “Type Aliases”TemplateErrorCode
Section titled “TemplateErrorCode”export type TemplateErrorCode =
| "PARSE"
| "RENDER"
| "LIMIT_EXCEEDED"
| "TEMPLATE_NOT_FOUND"
| "INVALID_NAME"
| "INVALID_DATA";
Error codes reported on Error.code by vael:template operations.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** @since 0.0.0-alpha.0 */
readonly render: typeof render;
/** @since 0.0.0-alpha.0 */
readonly compile: typeof compile;
/** @since 0.0.0-alpha.0 */
readonly createTemplateSet: typeof createTemplateSet;
/** @since 0.0.0-alpha.0 */
readonly escapeHtml: typeof escapeHtml;
};
export default _default;
Default export mirroring render, compile, createTemplateSet, and escapeHtml.
