A dynamic content html processing library for template interpolation with rich content support
npm install dynamic-content-htmlA powerful and lightweight utility library for Dynamic Content Html processing with template interpolation and rich content support. Designed specifically for translation libraries, i18n frameworks, and dynamic email systems. Perfect for email editors like Unlayer, GrapesJS, Canva, and any scenario where you need flexible text processing.
- 🚀 Simple API - Similar to i18n libraries with d() and d.rich() functions
- 🔧 Configurable Format - Support {var}, {{var}}, [var], [[var]], or any custom format
- 📧 Email Template Support - Perfect for dynamic email content processing
- 🎨 Rich Content - Support for HTML functions and complex content processing
- 🌐 Universal - Works with React, Vue, Node.js, and vanilla JavaScript
- 📦 Lightweight - Small bundle size with no external dependencies
- ✅ Well Tested - Comprehensive test coverage
- 🔌 Easy Integration - Drop-in replacement for existing text processing
``bash`
npm install dynamic-content-html
`bash`
yarn add dynamic-content-html
`bash`
pnpm add dynamic-content-html
`javascript
import d from 'dynamic-content-html';
// Simple variable replacement
const message = d('Hello {name}!', { name: 'Jane' });
console.log(message); // "Hello Jane!"
// Multiple variables
const welcome = d('Welcome {name}, you have {count} messages', {
name: 'John',
count: 5
});
console.log(welcome); // "Welcome John, you have 5 messages"
// Direct variables (no object wrapper needed)
const greeting = d('Hello {firstName}!', { firstName: 'Jane' });
console.log(greeting); // "Hello Jane!"
// Nested object properties
const user = { firstName: 'John', lastName: 'Doe' };
const fullName = d('Hello {user.firstName} {user.lastName}!', { user });
console.log(fullName); // "Hello John Doe!"
// Deep nested properties
const data = {
user: {
profile: {
personal: { firstName: 'John', lastName: 'Doe' },
contact: { email: 'john@example.com' }
}
}
};
const deepTemplate = d('User: {user.profile.personal.firstName}, Email: {user.profile.contact.email}', data);
console.log(deepTemplate); // "User: John, Email: john@example.com"
`
`javascript
// Double curly braces (like Handlebars/Mustache)
const message1 = d('Hello {{name}}!', { name: 'Jane' }, {
variableFormat: { start: '{{', end: '}}' }
});
// Square brackets (like some email platforms)
const message2 = d('Hello [name]!', { name: 'Jane' }, {
variableFormat: { start: '[', end: ']' }
});
// Double square brackets (like some CMS systems)
const message3 = d('Hello [[name]]!', { name: 'Jane' }, {
variableFormat: { start: '[[', end: ']]' }
});
// Custom format (like HTML-like tags)
const message4 = d('Hello
variableFormat: { start: '<', end: '>' }
});
`
`javascript
import d from 'dynamic-content-html';
const recipient = {
firstName: 'John',
lastName: 'Doe',
email: 'john@example.com',
phoneNumber: '+1234567890',
address: {
street: '123 Main St',
city: 'New York',
state: 'NY',
zipCode: '10001'
}
};
// Process email template with recipient data
const emailTemplate =
Dear {recipient.firstName} {recipient.lastName},
Thank you for your order! We'll send updates to {recipient.email}.
Shipping Address:
{recipient.address.street}
{recipient.address.city}, {recipient.address.state} {recipient.address.zipCode}
Best regards,
The Team;
const personalizedEmail = d(emailTemplate, { recipient });
console.log(personalizedEmail);
`
`javascript
import d from 'dynamic-content-html';
// Rich content with HTML functions (default format)
const richMessage = d.rich('Click {guidelines} for more information', {
guidelines: (chunks) => ${chunks}
});
console.log(richMessage); // "Click guidelines for more information"
// Rich content with variables
const welcomeEmail = d.rich(
Hello {user.firstName},
Please click {verifyLink} to verify your email address.
If you have any questions, contact us at {supportLink}., {${chunks}
user: { firstName: 'Jane' },
verifyLink: (chunks) => ,${chunks}
supportLink: (chunks) =>
});
// Rich content with custom format (for email platforms)
const emailTemplate = d.rich('Click [[button]] to continue', {
button: (chunks) => `
}, { variableFormat: { start: '[[', end: ']]' } });
`javascript
import d from 'dynamic-content-html';
// Basic MJML text content processing
const mjmlText = d.mjml('
console.log(mjmlText); // "
// MJML with attributes
const mjmlButton = d.mjml('
url: 'https://example.com'
});
console.log(mjmlButton); // "
// Complex MJML email template
const emailTemplate =
Hello {user.name}!
Welcome to {company.name}. Your order #{order.id} has been confirmed.
Track Your Order
;
const data = {
emailTitle: 'Order Confirmation',
textColor: '#333333',
user: { name: 'John Smith' },
company: { name: 'MyStore' },
order: {
id: '12345',
trackingUrl: 'https://mystore.com/track/12345'
},
buttonColor: '#007bff'
};
const processedEmail = d.mjml(emailTemplate, data);
console.log(processedEmail);
// Output: Complete MJML with all variables replaced
// MJML with custom variable format
const customMjml = d.mjml('
{ name: 'Jane' },
{ variableFormat: { start: '[[', end: ']]' } }
);
console.log(customMjml); // "
// MJML with nested object properties
const userData = {
user: {
profile: {
name: 'Alice',
location: { city: 'New York' }
}
}
};
const nestedMjml = d.mjml(
'
userData
);
console.log(nestedMjml); // "
`
Processes a template string with variable interpolation.
Parameters:
- template (string): Template string with {{variable}} placeholdersoptions
- (DynamicTextOptions): Object containing variable values
Returns: Processed string with variables replaced
Example:
`javascript`
d('Hello {{name}}!', { name: 'World' }) // "Hello World!"
Processes a template string with variable interpolation and rich content functions.
Parameters:
- template (string): Template string with {{variable}} placeholdersoptions
- (RichTextOptions): Object containing variable values and rich content functions
Returns: Processed string with variables replaced and rich content applied
Example:
`javascript${chunks}
d.rich('Click {{link}}', {
link: (chunks) => `
}) // "Click link"
Processes MJML content with dynamic data, handling both MJML attributes and text content.
Parameters:
- template (string): MJML template string with variable placeholdersoptions
- (MJMLOptions): Object containing variable valuesconfig
- (MJMLConfig, optional): Configuration for variable format and MJML processing
Returns: Processed MJML string with variables replaced
Example:
`javascript
d.mjml('
// "
d.mjml('
// "
`
The library provides built-in support for recipient objects with the following properties:
`typescript`
interface Recipient {
first_name?: string;
last_name?: string;
full_name?: string; // Auto-generated if not provided
email?: string;
phone_number?: string;
street?: string;
city?: string;
state?: string;
zip_code?: string;
}
`typescript
import d, { Recipient, DynamicTextOptions, RichTextOptions, MJMLOptions, MJMLConfig } from 'dynamic-content-html';
const recipient: Recipient = {
first_name: 'John',
last_name: 'Doe'
};
const options: DynamicTextOptions = {
recipient,
message: 'Welcome!'
};
const result = d('Hello {{recipient.full_name}}, {{message}}', options);
`
#### React i18next Integration
`jsx
import React from 'react';
import { useTranslation } from 'react-i18next';
import d from 'dynamic-content-html';
const WelcomeComponent = ({ user }) => {
const { t } = useTranslation();
// Use dynamic-content-html for complex translations
const welcomeMessage = d(t('welcome.message'), {
user: {
firstName: user.firstName,
lastName: user.lastName
}
});
return
// Translation files (en.json)
{
"welcome": {
"message": "Hello {user.firstName} {user.lastName}! Welcome to our platform."
}
}
`
#### Vue i18n Integration
`vue
`
#### Unlayer Integration
`javascript
import d from 'dynamic-content-html';
// Process Unlayer email template
function processUnlayerTemplate(template, recipientData) {
// Unlayer uses {{variable}} format
return d(template, recipientData, {
variableFormat: { start: '{{', end: '}}' }
});
}
// Example usage
const unlayerTemplate =
;const processed = processUnlayerTemplate(unlayerTemplate, {
recipient: { firstName: 'John' },
order: { id: '12345', trackingUrl: '/track/12345' }
});
`#### GrapesJS Integration
`javascript
import d from 'dynamic-content-html';// Process GrapesJS email template
function processGrapesJSTemplate(template, data) {
// GrapesJS might use [variable] format
return d(template, data, {
variableFormat: { start: '[', end: ']' }
});
}
// Example usage
const grapesTemplate =
You have [notifications.count] new messages.
;const processed = processGrapesJSTemplate(grapesTemplate, {
user: { name: 'Jane' },
notifications: { count: 5 }
});
`#### Canva Integration
`javascript
import d from 'dynamic-content-html';// Process Canva design template
function processCanvaTemplate(template, data) {
// Canva might use [[variable]] format
return d(template, data, {
variableFormat: { start: '[[', end: ']]' }
});
}
// Example usage
const canvaTemplate =
Welcome [[user.firstName]]!
Visit us at [[company.website]]
;const processed = processCanvaTemplate(canvaTemplate, {
company: { name: 'My Company', website: 'https://mycompany.com' },
user: { firstName: 'John' }
});
`$3
#### React
`jsx
import React from 'react';
import d from 'dynamic-content-html';const WelcomeEmail = ({ user, message }) => {
const emailContent = d(
{message}
, { user, message }); return
;
};
`#### Vue
`vue
`#### Node.js
`javascript
const d = require('dynamic-content-html');// Server-side email processing
function sendWelcomeEmail(user) {
const emailTemplate =
; const personalizedEmail = d(emailTemplate, { user });
// Send email...
console.log(personalizedEmail);
}
`Advanced Usage
$3
`javascript
// Direct variables (flat structure)
const message1 = d('Hello {{firstName}}!', { firstName: 'Jane' });
console.log(message1); // "Hello Jane!"// Nested object properties
const user = {
profile: {
name: 'John Doe',
preferences: {
theme: 'dark'
}
}
};
const message2 = d('Hello {{user.profile.name}}, your theme is {{user.profile.preferences.theme}}', { user });
console.log(message2); // "Hello John Doe, your theme is dark"
// Mixed approach - both direct and nested
const data = {
firstName: 'Jane', // Direct variable
user: { // Nested object
profile: {
email: 'jane@example.com'
}
}
};
const mixedMessage = d('Hello {{firstName}}, your email is {{user.profile.email}}', data);
console.log(mixedMessage); // "Hello Jane, your email is jane@example.com"
// Complex nested structures
const orderData = {
customer: {
personal: {
firstName: 'John',
lastName: 'Doe'
},
contact: {
email: 'john@example.com',
phone: '+1234567890'
}
},
order: {
id: '12345',
items: ['item1', 'item2'],
total: 99.99
}
};
const orderTemplate =
;const orderMessage = d(orderTemplate, orderData);
console.log(orderMessage);
`$3
`javascript
const emailTemplate = {{content}}
;const richEmail = d.rich(emailTemplate, {
title: 'Welcome!',
content: 'Thank you for joining us.',
action_button: (chunks) =>
,
footer_link: (chunks) => ${chunks}
});
`$3
`javascript
// Handle missing variables gracefully
const template = 'Hello {{name}}, you have {{count}} messages';
const result = d(template, { name: 'John' }); // count is missing
console.log(result); // "Hello John, you have messages"// Handle null/undefined values
const result2 = d('Hello {{name}}!', { name: null });
console.log(result2); // "Hello !"
`Development
$3
`bash
npm run build
`$3
`bash
npm test
``bash
npm run test:watch
``bash
npm run test:coverage
`$3
`bash
npm run dev
`Contributing
1. Fork the repository
2. Create your feature branch (
git checkout -b feature/amazing-feature)
3. Commit your changes (git commit -m 'Add some amazing feature')
4. Push to the branch (git push origin feature/amazing-feature)
5. Open a Pull RequestLicense
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
$3
- Initial release
- Basic template interpolation
- Recipient object support
- Rich content processing
- TypeScript support
- Comprehensive test coverageWhy Choose Dynamic Content Html?
$3
- Drop-in replacement for existing i18n interpolation
- Custom format support to match any translation system
- Rich content processing for complex multilingual content
- Nested object support for complex translation data$3
- Email template processing with flexible variable formats
- Rich HTML content generation for email campaigns
- Integration ready for Unlayer, GrapesJS, Canva
- Recipient data processing for personalized emails$3
- Simple API similar to existing i18n libraries
- TypeScript support with full type definitions
- Lightweight - no external dependencies
- Well tested with comprehensive coverageReal-World Use Cases
$3
`javascript
// Process email templates from various sources
const unlayerTemplate = processUnlayerTemplate(template, recipientData);
const grapesTemplate = processGrapesJSTemplate(template, recipientData);
const canvaTemplate = processCanvaTemplate(template, recipientData);
`$3
`javascript
// Handle complex translations with nested data
const message = d(t('welcome.complex'), {
user: { profile: { name: 'John' } },
order: { items: [{ name: 'Product 1' }] }
});
`$3
`javascript
// Process dynamic email content
const emailContent = d.rich(template, {
recipient: userData,
actionButton: (text) => ,
unsubscribeLink: (text) => ${text}
});
``If you have any questions, need help, or want to discuss integration:
- 📧 Email: code4change.co@gmail.com
- 📚 Documentation: Check the API Reference
- 🐛 Issues: GitHub Issues
- 💡 Feature Requests: GitHub Discussions
We welcome contributions! Whether you're:
- Adding support for new email platforms
- Improving i18n library integrations
- Enhancing performance
- Adding new features
Please see our Contributing Guide for details.
---
Dynamic Content Html - The ultimate utility for translation libraries and dynamic email systems! 🚀
Made with ❤️ for developers who need flexible text processing