or run

npx @tessl/cli init
Log in

Version

Files

docs

index.md
tile.json

task.mdevals/scenario-6/

Template Renderer

Build a reusable template compiler for HTML/text snippets with configurable delimiters and debugging options.

Capabilities

Escaped rendering

  • Compiling <p><%- name %></p> and rendering with { name: "<script>" } produces <p>&lt;script&gt;</p> and a second render with { name: "Ada" } reuses the compiled function to produce <p>Ada</p>. @test

Custom delimiters

  • When options specify {{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>&lt;b&gt;Hi&lt;/b&gt;</section>. @test

Conditional and loop sections

  • A template that branches on items.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

Imports and explicit data variable

  • Compiling <%= 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" }. @test

Implementation

@generates

API

export 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;

Dependencies { .dependencies }

lodash { .dependency }

Provides template compilation with escape/interpolate/evaluate delimiters, configurable imports, and source mapping hints. @satisfied-by