0.1.32 • Published 8 months ago
@ackplus/nest-dynamic-templates v0.1.32
@nestjs/dynamic-templates
A dynamic template management system for NestJS applications. This package provides a flexible way to manage and render templates with support for multiple template engines and language formats.
Features
- Support for multiple template engines (Nunjucks, Handlebars, EJS, Pug)
- Support for multiple language formats (MJML, HTML, Markdown, Text)
- Template layouts with inheritance
- Scoped templates with fallback to system templates
- Locale support with fallback
- Template validation
- TypeScript support
Installation
npm install @nestjs/dynamic-templatesQuick Start
- Import the module in your
app.module.ts:
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { NestDynamicTemplatesModule } from '@nestjs/dynamic-templates';
@Module({
imports: [
TypeOrmModule.forRoot({
// your database configuration
}),
NestDynamicTemplatesModule
]
})
export class AppModule {}- Use the services in your application:
import { Injectable } from '@nestjs/common';
import { TemplateService } from '@nestjs/dynamic-templates';
import { TemplateTypeEnum } from '@nestjs/dynamic-templates';
@Injectable()
export class EmailService {
constructor(private readonly templateService: TemplateService) {}
async sendWelcomeEmail(user: any) {
const result = await this.templateService.renderTemplate({
type: TemplateTypeEnum.EMAIL,
name: 'welcome',
scope: 'user',
scopeId: user.id,
locale: user.locale,
context: {
user,
appName: 'My App'
}
});
// Send email using your email service
await this.emailService.send({
to: user.email,
subject: result.subject,
html: result.content
});
}
}Template Creation
Create a template using the TemplateService:
await templateService.createTemplate({
name: 'welcome',
type: TemplateTypeEnum.EMAIL,
format: TemplateFormatEnum.NUNJUCKS,
languageFormat: LanguageFormatEnum.MJML,
content: `
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text>
Welcome to {{ appName }}, {{ user.name }}!
</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
`,
subject: 'Welcome to {{ appName }}',
scope: 'user',
scopeId: '123',
locale: 'en'
});Template Layouts
Create a layout template:
await templateLayoutService.createLayout({
name: 'email-base',
format: TemplateFormatEnum.NUNJUCKS,
languageFormat: LanguageFormatEnum.MJML,
content: `
<mjml>
<mj-head>
<mj-title>{{ title }}</mj-title>
</mj-head>
<mj-body>
<mj-section>
<mj-column>
<mj-text>
{{ content }}
</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
`,
scope: 'system'
});Use the layout in a template:
await templateService.createTemplate({
name: 'welcome',
type: TemplateTypeEnum.EMAIL,
format: TemplateFormatEnum.NUNJUCKS,
languageFormat: LanguageFormatEnum.MJML,
content: 'Welcome to {{ appName }}, {{ user.name }}!',
subject: 'Welcome to {{ appName }}',
templateLayoutName: 'email-base',
scope: 'user',
scopeId: '123',
locale: 'en'
});API Reference
TemplateService
renderTemplate(data: RenderTemplateDto): Promise<TemplateOutputDTO>createTemplate(data: CreateTemplateDto): Promise<NestDynamicTemplate>updateTemplate(data: UpdateTemplateDto): Promise<NestDynamicTemplate>deleteTemplate(id: string): Promise<void>findTemplate(id: string): Promise<NestDynamicTemplate>findTemplates(type?: TemplateTypeEnum, scope?: string, scopeId?: string, locale?: string): Promise<NestDynamicTemplate[]>
TemplateLayoutService
renderLayout(data: RenderTemplateLayoutDto): Promise<string>createLayout(data: CreateTemplateLayoutDto): Promise<TemplateLayout>updateLayout(data: UpdateTemplateLayoutDto): Promise<TemplateLayout>deleteLayout(id: string): Promise<void>findLayout(id: string): Promise<TemplateLayout>findLayouts(scope?: string, scopeId?: string, locale?: string): Promise<TemplateLayout[]>
License
MIT
0.1.32
8 months ago
0.1.31
8 months ago
0.1.30
8 months ago
0.1.29
8 months ago
0.1.28
8 months ago
0.1.27
8 months ago
0.1.26
8 months ago
0.1.25
8 months ago
0.1.24
8 months ago
0.1.23
8 months ago
0.1.22
8 months ago
0.1.21
8 months ago
0.1.20
8 months ago
0.1.19
8 months ago
0.1.18
8 months ago
0.1.17
8 months ago
0.1.16
8 months ago
0.1.15
8 months ago
0.1.14
8 months ago
0.1.13
8 months ago
0.1.12
8 months ago
0.1.10
8 months ago
0.1.9
8 months ago
0.1.8
8 months ago
0.1.7
8 months ago
0.1.6
8 months ago
0.1.5
8 months ago
0.1.4
8 months ago
0.1.3
8 months ago
0.1.2
8 months ago
0.1.1
8 months ago
0.1.0
8 months ago