{"id":13130,"library":"es6-dynamic-template","title":"ES6 Dynamic Templates","description":"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.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/mikemaccana/dynamic-template","tags":["javascript"],"install":[{"cmd":"npm install es6-dynamic-template","lang":"bash","label":"npm"},{"cmd":"yarn add es6-dynamic-template","lang":"bash","label":"yarn"},{"cmd":"pnpm add es6-dynamic-template","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is primarily designed for CommonJS environments.","symbol":"fillTemplate","correct":"const fillTemplate = require('es6-dynamic-template');"},{"note":"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.","wrong":"import { fillTemplate } from 'es6-dynamic-template';","symbol":"fillTemplate","correct":"import fillTemplate from 'es6-dynamic-template';"}],"quickstart":{"code":"const fillTemplate = require('es6-dynamic-template');\n\n// Basic usage with a simple greeting\nconst greetingTemplate = 'Hi ${firstName}, welcome to ${city}!';\nconst userData = { firstName: 'Alice', city: 'New York' };\nconst filledGreeting = fillTemplate(greetingTemplate, userData);\nconsole.log(filledGreeting);\n// Expected output: Hi Alice, welcome to New York!\n\n// Using with a more complex object structure\nconst productTemplate = 'Product: ${product.name} (ID: ${product.id}) - Price: $${product.price.toFixed(2)}';\nconst productData = {\n  product: {\n    name: 'Dynamic Widget',\n    id: 'W12345',\n    price: 99.998\n  }\n};\nconst filledProductInfo = fillTemplate(productTemplate, productData);\nconsole.log(filledProductInfo);\n// Expected output: Product: Dynamic Widget (ID: W12345) - Price: $100.00\n\n// Handling missing variables (they remain as-is or evaluate to 'undefined' in native templates)\nconst incompleteTemplate = 'Hello ${user.name}! Your age is ${user.age}.';\nconst incompleteData = { user: { name: 'Bob' } };\nconst filledIncomplete = fillTemplate(incompleteTemplate, incompleteData);\nconsole.log(filledIncomplete);\n// Expected output: Hello Bob! Your age is ${user.age}.","lang":"javascript","description":"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."},"warnings":[{"fix":"No direct fix needed, but review template behavior for highly complex strings if migrating from v1.","message":"Version 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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always pass the template string using single quotes (`'...'`) or double quotes (`\"...\"`) to `fillTemplate`.","message":"Template 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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`.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"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`.","error":"TypeError: Cannot read properties of undefined (reading 'variableName') (or similar)"},{"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 ...`.","cause":"The `es6-dynamic-template` package is a CommonJS module that exports its main function as a default. It does not provide named exports.","error":"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 { ... }`)."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}