@suaveplan/templates - Plugin-based template engine with type-safe contexts, variable interpolation, transform and control-flow plugins, and preset bundles.
Summary
A plugin-based string template engine for TypeScript. Resolves {variable} and {variable:transform} tokens in strings using a priority-sorted, regex-scan pipeline — no eval, no AST parser overhead. Designed for filling templates in notification messages, email subjects, file name patterns, code generation, and similar string-expansion tasks. Ships nine built-in plugins: dot-notation and array-index variable resolution; case, regex, and split transforms; counter, datetime, and UUID dynamic generators; and conditional/loop control flow. Three preset bundles (minimal, standard, full) let you trade features for bundle size. The injectable Clock and Rng make every rendering call deterministic in tests. Do not confuse with @suaveplan/i18n, which handles locale-keyed message catalogs and plural forms — use this package when your source strings are already in the target language and you only need token substitution.
Documentation tree
- ParseState — Provides factory functions and mutation helpers for
ParseState — the stateful object that the TemplateEngine passes…
- TemplateEngine — The
TemplateEngine class is the central orchestrator of @suaveplan/templates. It manages a priority-sorted registry…
- apply-plugin — Provides the core two-phase plugin application logic used by
TemplateEngine for both synchronous and asynchronous ren…
- error-codes — Defines and freezes the complete error-code registry for
@suaveplan/templates using the defineCodes utility from `@…
- template-error — Defines the structured error hierarchy for
@suaveplan/templates. All errors extend SuaveplanError from `@suaveplan/…
- otel — (no submodules)
- ConditionalPlugin — Conditionally includes or excludes blocks of template content based on context values. The plugin evaluates a condition…
- LoopPlugin — Iterates over an array in the context object and renders a sub-template once for each element, concatenating the result…
- CounterPlugin — Provides stateful auto-incrementing counters within a single template rendering pass. Counters are scoped to the `Parse…
- DateTimePlugin — Injects current date and time values into templates using a flexible, token-based formatting syntax. Because date-time…
- UuidPlugin — Generates UUID v4 and URL-safe nano-ID strings inline within templates. Because ID generation depends on `crypto.random…
- CaseTransformPlugin — Applies string case transformations to resolved context values. Supports eight case styles covering all common naming c…
- RegexPlugin — Applies inline regular-expression operations to resolved context values. The plugin enables match extraction, capture g…
- StringSplitPlugin — Splits a resolved string value on a configurable delimiter and returns a single element by index. This plugin solves th…
- ArrayIndexPlugin — Resolves bracket-notation array-index references in templates. Handles
{items[0]}, {items[-1]}, and nested `{users[…
- DotNotationPlugin — Resolves simple variable references using dot-notation paths. This is the primary workhorse plugin for template renderi…
- full — Provides the complete plugin suite in two variants:
fullPreset() matches standardPreset() exactly — it is the recom…
- minimal — Provides the lightest possible
PluginPreset for the TemplateEngine. It bundles only two plugins — array-index and…
- standard — Provides a comprehensive
PluginPreset suitable for the majority of production use cases. It bundles all non-loop plug…
- schemas — (no submodules)
- types — (no submodules)
- escape — Provides three utility functions for handling escape sequences in template strings. These are low-level primitives used…
- format — Provides core formatting utilities used by the dynamic plugins: zero-padded number/string padding, date formatting with…
- nested-value — Provides three functions for traversing, checking, and mutating plain object trees using dot-notation or bracket-notati…
- [templates-otel](./src/otel/templates-otel.md) — _(no submodules)_- [ConditionalPlugin](./src/plugins/control/ConditionalPlugin.md) — Conditionally includes or excludes blocks of template content based on context values. The plugin evaluates a condition…
- LoopPlugin — Iterates over an array in the context object and renders a sub-template once for each element, concatenating the result…
- CounterPlugin — Provides stateful auto-incrementing counters within a single template rendering pass. Counters are scoped to the `Parse…
- DateTimePlugin — Injects current date and time values into templates using a flexible, token-based formatting syntax. Because date-time…
- UuidPlugin — Generates UUID v4 and URL-safe nano-ID strings inline within templates. Because ID generation depends on `crypto.random…
- CaseTransformPlugin — Applies string case transformations to resolved context values. Supports eight case styles covering all common naming c…
- RegexPlugin — Applies inline regular-expression operations to resolved context values. The plugin enables match extraction, capture g…
- StringSplitPlugin — Splits a resolved string value on a configurable delimiter and returns a single element by index. This plugin solves th…
- ArrayIndexPlugin — Resolves bracket-notation array-index references in templates. Handles
{items[0]}, {items[-1]}, and nested `{users[…
- DotNotationPlugin — Resolves simple variable references using dot-notation paths. This is the primary workhorse plugin for template renderi…
- full — Provides the complete plugin suite in two variants:
fullPreset() matches standardPreset() exactly — it is the recom…
- minimal — Provides the lightest possible
PluginPreset for the TemplateEngine. It bundles only two plugins — array-index and…
- standard — Provides a comprehensive
PluginPreset suitable for the majority of production use cases. It bundles all non-loop plug…
- schemas — (no submodules)- templates.schemas — (no submodules)- types — (no submodules)- index — (no submodules)- escape — Provides three utility functions for handling escape sequences in template strings. These are low-level primitives used…
- format — Provides core formatting utilities used by the dynamic plugins: zero-padded number/string padding, date formatting with…
- nested-value — Provides three functions for traversing, checking, and mutating plain object trees using dot-notation or bracket-notati…
- templates-otel — (no submodules)- ConditionalPlugin — Conditionally includes or excludes blocks of template content based on context values. The plugin evaluates a condition…
- LoopPlugin — Iterates over an array in the context object and renders a sub-template once for each element, concatenating the result…
- CounterPlugin — Provides stateful auto-incrementing counters within a single template rendering pass. Counters are scoped to the `Parse…
- DateTimePlugin — Injects current date and time values into templates using a flexible, token-based formatting syntax. Because date-time…
- UuidPlugin — Generates UUID v4 and URL-safe nano-ID strings inline within templates. Because ID generation depends on `crypto.random…
- CaseTransformPlugin — Applies string case transformations to resolved context values. Supports eight case styles covering all common naming c…
- RegexPlugin — Applies inline regular-expression operations to resolved context values. The plugin enables match extraction, capture g…
- StringSplitPlugin — Splits a resolved string value on a configurable delimiter and returns a single element by index. This plugin solves th…
- ArrayIndexPlugin — Resolves bracket-notation array-index references in templates. Handles
{items[0]}, {items[-1]}, and nested `{users[…
- DotNotationPlugin — Resolves simple variable references using dot-notation paths. This is the primary workhorse plugin for template renderi…
- full — Provides the complete plugin suite in two variants:
fullPreset() matches standardPreset() exactly — it is the recom…
- minimal — Provides the lightest possible
PluginPreset for the TemplateEngine. It bundles only two plugins — array-index and…
- standard — Provides a comprehensive
PluginPreset suitable for the majority of production use cases. It bundles all non-loop plug…
- schemas — (no submodules)- templates.schemas — (no submodules)- types — (no submodules)- index — (no submodules)- escape — Provides three utility functions for handling escape sequences in template strings. These are low-level primitives used…
- format — Provides core formatting utilities used by the dynamic plugins: zero-padded number/string padding, date formatting with…
- nested-value — Provides three functions for traversing, checking, and mutating plain object trees using dot-notation or bracket-notati…
- templates-otel — (no submodules)- ConditionalPlugin — Conditionally includes or excludes blocks of template content based on context values. The plugin evaluates a condition…
- LoopPlugin — Iterates over an array in the context object and renders a sub-template once for each element, concatenating the result…
- CounterPlugin — Provides stateful auto-incrementing counters within a single template rendering pass. Counters are scoped to the `Parse…
- DateTimePlugin — Injects current date and time values into templates using a flexible, token-based formatting syntax. Because date-time…
- UuidPlugin — Generates UUID v4 and URL-safe nano-ID strings inline within templates. Because ID generation depends on `crypto.random…
- CaseTransformPlugin — Applies string case transformations to resolved context values. Supports eight case styles covering all common naming c…
- RegexPlugin — Applies inline regular-expression operations to resolved context values. The plugin enables match extraction, capture g…
- StringSplitPlugin — Splits a resolved string value on a configurable delimiter and returns a single element by index. This plugin solves th…
- ArrayIndexPlugin — Resolves bracket-notation array-index references in templates. Handles
{items[0]}, {items[-1]}, and nested `{users[…
- DotNotationPlugin — Resolves simple variable references using dot-notation paths. This is the primary workhorse plugin for template renderi…
- full — Provides the complete plugin suite in two variants:
fullPreset() matches standardPreset() exactly — it is the recom…
- minimal — Provides the lightest possible
PluginPreset for the TemplateEngine. It bundles only two plugins — array-index and…
- standard — Provides a comprehensive
PluginPreset suitable for the majority of production use cases. It bundles all non-loop plug…
- schemas — (no submodules)- templates.schemas — (no submodules)- types — (no submodules)- index — (no submodules)- escape — Provides three utility functions for handling escape sequences in template strings. These are low-level primitives used…
- format — Provides core formatting utilities used by the dynamic plugins: zero-padded number/string padding, date formatting with…
- nested-value — Provides three functions for traversing, checking, and mutating plain object trees using dot-notation or bracket-notati…
- templates-otel — (no submodules)- ConditionalPlugin — Conditionally includes or excludes blocks of template content based on context values. The plugin evaluates a condition…
- LoopPlugin — Iterates over an array in the context object and renders a sub-template once for each element, concatenating the result…
- CounterPlugin — Provides stateful auto-incrementing counters within a single template rendering pass. Counters are scoped to the `Parse…
- DateTimePlugin — Injects current date and time values into templates using a flexible, token-based formatting syntax. Because date-time…
- UuidPlugin — Generates UUID v4 and URL-safe nano-ID strings inline within templates. Because ID generation depends on `crypto.random…
- CaseTransformPlugin — Applies string case transformations to resolved context values. Supports eight case styles covering all common naming c…
- RegexPlugin — Applies inline regular-expression operations to resolved context values. The plugin enables match extraction, capture g…
- StringSplitPlugin — Splits a resolved string value on a configurable delimiter and returns a single element by index. This plugin solves th…
- ArrayIndexPlugin — Resolves bracket-notation array-index references in templates. Handles
{items[0]}, {items[-1]}, and nested `{users[…
- DotNotationPlugin — Resolves simple variable references using dot-notation paths. This is the primary workhorse plugin for template renderi…
- full — Provides the complete plugin suite in two variants:
fullPreset() matches standardPreset() exactly — it is the recom…
- minimal — Provides the lightest possible
PluginPreset for the TemplateEngine. It bundles only two plugins — array-index and…
- standard — Provides a comprehensive
PluginPreset suitable for the majority of production use cases. It bundles all non-loop plug…
- schemas — (no submodules) - templates.schemas — (no submodules)- types — (no submodules) - index — (no submodules)- escape — Provides three utility functions for handling escape sequences in template strings. These are low-level primitives used…
- format — Provides core formatting utilities used by the dynamic plugins: zero-padded number/string padding, date formatting with…
- nested-value — Provides three functions for traversing, checking, and mutating plain object trees using dot-notation or bracket-notati…
Features
- Priority-sorted plugin pipeline (CRITICAL → HIGH → MEDIUM → LOW → MINIMAL)
- 9 built-in plugins covering variables, transforms, dynamic generation, and control flow
- 3 preset bundles (minimal, standard, full)
- Synchronous and asynchronous rendering paths
compile() for reusable template objects
- Injectable
Clock and Rng for deterministic testing
ParseState per render pass — counters, cache, custom plugin state
- Zod schemas for runtime configuration validation
- 100% test coverage on all modules
Installation
Quick Start
Plugins
Variable Resolution
Transforms
Dynamic
Control Flow
Presets
Engine API
Types & Schemas
Utilities
| Symbol |
Module |
| nested-value |
getNestedValue, hasNestedPath, setNestedValue |
| format |
pad, formatDate, generateUuid, generateNanoid |
| escape |
escapeTemplate, unescapeTemplate |
Errors
Writing a Custom Plugin
Loop Support
Loops require injecting the engine's internal render function: