Registry / serialization / es6-dynamic-template

es6-dynamic-template

JSON →
library2.0.0jsnpmunverified

The `es6-dynamic-template` package provides a utility for dynamically substituting variables into ES6-like template strings, enabling runtime template construction without hardcoding a function for every possible string. It addresses the complexities of the `this` context within native template literals, allowing for late resolution of variables. The current stable version is 2.0.0, released after a significant update that moved from an `eval`-based parsing approach in v1 to a safer regex-based method in v2, while maintaining the same API. Its key differentiator lies in facilitating dynamic template generation, particularly useful in scenarios requiring numerous similar templates where the template string itself is variable. While not on a strict release cadence, the project offers a stable solution for a specific templating challenge.

npm install es6-dynamic-template
INSTALL
IMPORT
SIG · ES6-DYNAMIC-TEMPLA
E
es6-dynamic-template
serializationjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fillTemplate
const fillTemplate = require('es6-dynamic-template');
This package is primarily designed for CommonJS environments.
fillTemplate
import fillTemplate from 'es6-dynamic-template';
import { fillTemplate } from 'es6-dynamic-template';
For ESM environments, this CommonJS package's default export can be imported this way. Named imports are not supported as the function is the module's default export.

This quickstart demonstrates how to load `es6-dynamic-template` and use `fillTemplate` with various data structures, including nested objects and cases with missing variables, showing how to achieve dynamic string interpolation.

const fillTemplate = require('es6-dynamic-template'); // Basic usage with a simple greeting const greetingTemplate = 'Hi ${firstName}, welcome to ${city}!'; const userData = { firstName: 'Alice', city: 'New York' }; const filledGreeting = fillTemplate(greetingTemplate, userData); console.log(filledGreeting); // Expected output: Hi Alice, welcome to New York! // Using with a more complex object structure const productTemplate = 'Product: ${product.name} (ID: ${product.id}) - Price: $${product.price.toFixed(2)}'; const productData = { product: { name: 'Dynamic Widget', id: 'W12345', price: 99.998 } }; const filledProductInfo = fillTemplate(productTemplate, productData); console.log(filledProductInfo); // Expected output: Product: Dynamic Widget (ID: W12345) - Price: $100.00 // Handling missing variables (they remain as-is or evaluate to 'undefined' in native templates) const incompleteTemplate = 'Hello ${user.name}! Your age is ${user.age}.'; const incompleteData = { user: { name: 'Bob' } }; const filledIncomplete = fillTemplate(incompleteTemplate, incompleteData); console.log(filledIncomplete); // Expected output: Hello Bob! Your age is ${user.age}.
Debug
Known issues
breakingVersion 2.0.0 moved from an `eval`-based implementation to a regex-based approach. While maintaining the API, this change inherently mitigates security risks associated with `eval` for user-provided template strings but might alter edge-case parsing behavior for highly complex or malformed templates if specific `eval` quirks were relied upon in v1.
fix
No direct fix needed, but review template behavior for highly complex strings if migrating from v1.
affects: >=2.0.0
gotchaTemplate strings *must* use regular single or double quotes, not ES6 backticks (` `). Using backticks will result in the string being evaluated *before* `es6-dynamic-template` can process it, potentially embedding `undefined` variables or causing syntax errors if the variables are not in scope.
fix
Always pass the template string using single quotes (`'...'`) or double quotes (`"..."`) to `fillTemplate`.
affects: >=1.0.0
gotchaThe package does not ship with TypeScript type definitions. Users in TypeScript projects will need to provide their own ambient type declarations (e.g., a `d.ts` file) or use `@ts-ignore` to suppress type errors when importing and using the `fillTemplate` function.
fix
Create a `types/es6-dynamic-template.d.ts` file with `declare module 'es6-dynamic-template' { function fillTemplate(template: string, data: Record<string, any>): string; export = fillTemplate; }` or use `// @ts-ignore`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'variableName') (or similar)
A variable referenced in the template string (e.g., `${user.name}`) is expected to be nested within the `data` object, but an intermediate property (e.g., `user`) is `undefined` or `null`.
fix
Ensure all parts of the variable path within the template string correspond to existing properties in the `data` object, or provide default values in `data` to prevent undefined access.
SyntaxError: Named export 'fillTemplate' not found. The requested module 'es6-dynamic-template' does not provide an export named 'fillTemplate' (or similar errors when using `import { ... }`).
The `es6-dynamic-template` package is a CommonJS module that exports its main function as a default. It does not provide named exports.
fix
In ESM environments, import the module's default export: `import fillTemplate from 'es6-dynamic-template';`. Avoid using named import syntax like `import { fillTemplate } from ...`.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
es6-dynamic-template — npm install es6-dynamic-template · libregistry