@samveys/template

.tpl.html compiled to ES modules, no eval

Version
1.0.2Nov 5, 2026
Install
pnpm add @samveys/template
Import
import { compileTemplate, createTemplateEngine, escapeHtml, template, TEMPLATE_EXTENSION, SafeString } from '@samveys/template'

Function (4)

compileTemplate Function

Compiles template `source` to the source of an ES module. `file` is the name shown in errors. Throws `SVS_E109` with the file and line of the problem, e.g. a forbidden member such as `constructor`.

function compileTemplate(source: string, file: string): string;

Exported from packages/template/src/codegen.ts

createTemplateEngine Function

No documentation comment.

function createTemplateEngine(options: TemplateEngineOptions): TemplateEngine;

Exported from packages/template/src/engine.ts

escapeHtml Function

Escapes `& < > " ' \`` (and prints a {@link SafeString} unchanged). `null` and `undefined` print nothing.

function escapeHtml(value: unknown): string;

Exported from packages/template/src/runtime.ts

template Function

The `@samveys/template` plugin: registers the `template` engine with `@samveys/view`. Templates are `<viewsDir>/<name>.tpl.html` (`users.index` → `users/index.tpl.html`) and are compiled to ES modules in `cacheDir`; nothing is evaluated from strings.

function template(options?: TemplateOptions): import("@samveys/core").Plugin<TemplateOptions>;

Exported from packages/template/src/plugin.ts

Value (1)

TEMPLATE_EXTENSION Value

No documentation comment.

const TEMPLATE_EXTENSION = ".tpl.html";

Exported from packages/template/src/engine.ts

Class (1)

SafeString Class

HTML that is already safe: `{{ }}` prints it as is. Slots and sections are `SafeString`s.

class SafeString {
    readonly value: string;
    constructor(value: string);
    toString(): string;
}

Exported from packages/template/src/runtime.ts

Interface (3)

TemplateEngine Interface

No documentation comment.

interface TemplateEngine extends ViewEngine {
    /** Compiles a template without rendering it; returns the generated module source. */
    compile(name: string): Promise<string>;
}

Exported from packages/template/src/engine.ts

TemplateEngineOptions Interface

No documentation comment.

interface TemplateEngineOptions {
    /** Absolute directory with the templates. */
    readonly viewsDir: string;
    /** Absolute directory for the generated modules (`storage/views`). */
    readonly cacheDir: string;
    /** Sub-directory of `viewsDir` with components for `<x-name>`. Default `components`. */
    readonly componentsDir?: string;
    /** Recompile when the source changes. Default false; the plugin enables it when `app.env === 'local'`. */
    readonly watch?: boolean;
    /** Helpers filled in by other plugins (`vite`, `csrfToken`). Read at render time. */
    readonly helpers?: ViewHelpers;
}

Exported from packages/template/src/engine.ts

TemplateOptions Interface

No documentation comment.

interface TemplateOptions {
    /** Directory with the `.tpl.html` files. Default `<rootDir>/resources/views`. */
    readonly viewsDir?: string;
    /** Directory for the compiled modules. Default `<rootDir>/storage/views`. */
    readonly cacheDir?: string;
    /** Sub-directory of `viewsDir` with `<x-…>` components. Default `components`. */
    readonly componentsDir?: string;
    /** Recompile templates when their source changes. Default: on when the app environment is `local`. */
    readonly watch?: boolean;
}

Exported from packages/template/src/plugin.ts