template()
template(
template,data?):string
Replaces {key} placeholders in a string with values from a data object.
π Why is this a Hidden Gem?
Lightweight string interpolation with support for nested paths ({user.name}). No regex complexity, no external dependencies.
Supports nested paths ({user.name}). Use {{ and }} for literal braces. Missing keys become empty strings.
Parametersβ
template: stringβ
The template string with {key} placeholders.
data?: Record<string, unknown> = {}β
The data object containing values.
Returns: stringβ
The interpolated string.
Sinceβ
2.0.0
Performanceβ
O(n Γ m) time where n is template length, m is max path depth. Single regex pass with reduce for nested paths.
Also known asβ
template (Lodash, es-toolkit, Radashi, Antfu) Β· β (Remeda, Ramda, Effect, Modern Dash)
Exampleβ
template('Hello {name}', { name: 'World' });
// => 'Hello World'
template('{user.name} <{user.email}>', {
user: { name: 'John', email: 'john@example.com' }
});
// => 'John <john@example.com>'
template('Literal: {{escaped}}', {});
// => 'Literal: {escaped}'
How it works?β
Replaces {key} placeholders with values from a data object.
Supports nested paths.
Nested Pathsβ
Escape Bracesβ
Use {{ and }} for literal braces.
Missing Keysβ
Missing keys become empty strings.
Use Casesβ
Interpolate dynamic strings πβ
Replace placeholders in a string with data values. Perfect for localization (i18n) or generating dynamic messages.
const msg = template('Welcome, {user.name}!', { user: { name: 'Alice' } });
// 'Welcome, Alice!'
Render email notificationsβ
Build transactional email bodies from templates and user data. Avoids manual string concatenation and keeps templates readable.
const emailBody = template(
'Hi {name}, your order #{orderId} has been {status}. Track it at {trackingUrl}.',
{ name: 'Alice', orderId: '8842', status: 'shipped', trackingUrl: 'https://track.example.com/8842' }
);
// 'Hi Alice, your order #8842 has been shipped. Track it at https://track.example.com/8842.'
Format error messages with contextβ
Inject runtime context into error message templates for clearer debugging.
const errorTemplates = {
NOT_FOUND: 'Resource {resource} with id {id} not found.',
RATE_LIMIT: 'Rate limit exceeded for {endpoint}. Retry after {retryAfter}s.',
};
const msg = template(errorTemplates.NOT_FOUND, { resource: 'User', id: '42' });
// 'Resource User with id 42 not found.'
Generate SEO meta tags from page dataβ
Build dynamic meta titles and descriptions from page data. Essential for SSR apps and static site generators optimizing for search engines.
const metaTitle = template("{siteName} | {pageTitle}", {
siteName: "My Store",
pageTitle: "Running Shoes",
});
// => "My Store | Running Shoes"
const metaDescription = template(
"Buy {productName} at {price}. {category} with free shipping on orders over {freeShippingMin}.",
{ productName: "Nike Air Max", price: "$129", category: "Running Shoes", freeShippingMin: "$75" }
);
Build notification messages for multiple channelsβ
Generate notification text for push, SMS, and in-app from a single template. Perfect for multi-channel notification systems.
const templates = {
push: "{userName}, your order #{orderId} is {status}!",
sms: "Order #{orderId} {status}. Track: {trackingUrl}",
inApp: "Your order #{orderId} has been {status}. Check details.",
};
const data = { userName: "Alice", orderId: "4521", status: "shipped", trackingUrl: "https://track.co/4521" };
const pushMsg = template(templates.push, data);
const smsMsg = template(templates.sms, data);
const inAppMsg = template(templates.inApp, data);