docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
Build a reusable template compiler for HTML/text snippets with configurable delimiters and debugging options.
<p><%- name %></p> and rendering with { name: "<script>" } produces <p><script></p> and a second render with { name: "Ada" } reuses the compiled function to produce <p>Ada</p>. @test{{title}} for interpolation and {{- body}} for escaped output, compiling "<h1>{{title}}</h1><section>{{- body}}</section>" with { title: "News", body: "<b>Hi</b>" } returns <h1>News</h1><section><b>Hi</b></section>. @testitems.length and iterates items renders <ul><li>Ada</li><li>Lin</li></ul> for { items: ["Ada", "Lin"] } and No items when items is empty, using one compiled template. @test<%= format(ctx.user) %> from <%= ctx.city %> with an imports helper format and variable set to ctx renders User: Ada from London for { user: "Ada", city: "London" }. @testexport type TemplateSource = string;
export type TemplateDelimiters = {
interpolate?: RegExp | string;
escape?: RegExp | string;
evaluate?: RegExp | string;
};
export type TemplateOptions = {
delimiters?: TemplateDelimiters;
imports?: Record<string, unknown>;
variable?: string;
sourceURL?: string;
};
export type CompiledTemplate = {
render(context: Record<string, unknown>): string;
toString(): string;
};
export function compileTemplate(source: TemplateSource, options?: TemplateOptions): CompiledTemplate;
export function renderTemplate(source: TemplateSource, context: Record<string, unknown>, options?: TemplateOptions): string;Provides template compilation with escape/interpolate/evaluate delimiters, configurable imports, and source mapping hints. @satisfied-by