Skip to content
VaelJS Blog

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"
Since v0.0.0-alpha.0 vjs (V8) vjs-lite (QuickJS)
Cargo feature: vael-template (enabled by default in vaeljs-core and vaeljs-lite-core)
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>" }));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function render(
  source: string,
  data?: unknown,
  options?: TemplateOptions,
): string;

Compile and render a Go text/template once against optional JSON object data.

Parameters
NameTypeDescription
sourcestringGo text/template source text.
data optionalunknownOptional JSON object context (defaults to {}).
options optionalTemplateOptionsOptional Sprig, HTML-escaping, and resource limit options.
Returns
string — Rendered template output string.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: TemplateErrorCode.
import { render } from "vael:template";
console.log(render("Items: {{ len .items }}", { items: [1, 2, 3] }));
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function compile(
  source: string,
  options?: TemplateOptions,
): Template;

Compile a Go text/template once for repeated rendering.

Parameters
NameTypeDescription
sourcestringGo text/template source text.
options optionalTemplateOptionsOptional Sprig, HTML-escaping, and resource limit options.
Returns
Template — Pre-compiled Template handle.
Exceptions
  • TypeError — on wrong argument types.
  • Error — with code: TemplateErrorCode.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function createTemplateSet(options?: TemplateOptions): TemplateSet;

Create an empty TemplateSet for composing layouts and partials.

Parameters
NameTypeDescription
options optionalTemplateOptionsOptional Sprig, HTML-escaping, and resource limit options.
Returns
TemplateSet — Empty TemplateSet instance.
Exceptions
  • Error — with code "INVALID_DATA" on invalid options.
functionSince v0.0.0-alpha.0vjs & vjs-lite
export function escapeHtml(input: string): string;

Escape HTML special characters (&, <, >, ", ') in input.

Parameters
NameTypeDescription
inputstringPlain text string to HTML-escape.
Returns
string — HTML-escaped string.
Exceptions
  • Error — with code "INVALID_DATA" if input is not a string.
import { escapeHtml } from "vael:template";
console.log(escapeHtml("<script>")); // "&lt;script&gt;"
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
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.

Properties
PropertyTypeDescriptionSince
sprig optionalbooleanEnable safe deterministic Sprig template helpers (default: true).v0.0.0-alpha.0
hermetic optionalbooleanRestrict Sprig helpers to hermetic functions, omitting clock- and environment-reading helpers such as now and ago (default: false).v0.0.0-alpha.0
htmlEscape optionalbooleanAutomatically HTML-escape interpolated values unless marked with safeHtml (default: false).v0.0.0-alpha.0
maxSourceLength optionalnumberMaximum template source length in UTF-8 bytes (default: 1_048_576).v0.0.0-alpha.0
maxNestingDepth optionalnumberMaximum control-structure nesting depth (default: 100).v0.0.0-alpha.0
maxOutputLength optionalnumberMaximum rendered output length in UTF-8 bytes (default: 4_194_304).v0.0.0-alpha.0
maxIterations optionalnumberMaximum range loop iterations per render (default: 100_000).v0.0.0-alpha.0
maxFunctionCalls optionalnumberMaximum helper function calls per render (default: 100_000).v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
export interface Template {
  render(data?: unknown): string;
}

A pre-compiled Go text/template ready for repeated rendering.

Methods
methodSince v0.0.0-alpha.0
render(data?: unknown): string;

Render the compiled template against an optional JSON data object.

Parameters
NameTypeDescription
data optionalunknownOptional JSON object context (defaults to {}).
Returns
string — Rendered template output string.
Exceptions
  • Error — with code: TemplateErrorCode.
import { compile } from "vael:template";
const tpl = compile("Hi {{ .user }}");
console.log(tpl.render({ user: "Ada" }));
interfaceSince v0.0.0-alpha.0vjs & vjs-lite
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.

Methods
methodSince v0.0.0-alpha.0
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.

Parameters
NameTypeDescription
namestringUnique template name.
sourcestringGo text/template source text.
Returns
TemplateSet — this for chaining.
Exceptions
  • Error — with code "INVALID_NAME", "INVALID_DATA", "LIMIT_EXCEEDED", or "PARSE".
methodSince v0.0.0-alpha.0
render(name: string, data?: unknown): string;

Render the registered template name against optional JSON object data.

Parameters
NameTypeDescription
namestringRegistered template name to execute.
data optionalunknownOptional JSON object context (defaults to {}).
Returns
string — Rendered template output string.
Exceptions
  • Error — with code "TEMPLATE_NOT_FOUND", "INVALID_NAME", "INVALID_DATA", "LIMIT_EXCEEDED", "PARSE", or "RENDER".
methodSince v0.0.0-alpha.0
names(): string[];

Return the registered template names in sorted order.

Returns
string[] — Lexicographically sorted list of registered template names.
typeSince v0.0.0-alpha.0vjs & vjs-lite
export type TemplateErrorCode =
  | "PARSE"
  | "RENDER"
  | "LIMIT_EXCEEDED"
  | "TEMPLATE_NOT_FOUND"
  | "INVALID_NAME"
  | "INVALID_DATA";

Error codes reported on Error.code by vael:template operations.

defaultSince v0.0.0-alpha.0vjs & vjs-lite
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.