0.1.32 • Published 7 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
7 months ago
0.1.31
7 months ago
0.1.30
7 months ago
0.1.29
7 months ago
0.1.28
7 months ago
0.1.27
7 months ago
0.1.26
7 months ago
0.1.25
7 months ago
0.1.24
7 months ago
0.1.23
7 months ago
0.1.22
7 months ago
0.1.21
7 months ago
0.1.20
7 months ago
0.1.19
7 months ago
0.1.18
7 months ago
0.1.17
7 months ago
0.1.16
7 months ago
0.1.15
7 months ago
0.1.14
7 months ago
0.1.13
7 months ago
0.1.12
7 months ago
0.1.10
7 months ago
0.1.9
7 months ago
0.1.8
7 months ago
0.1.7
7 months ago
0.1.6
7 months ago
0.1.5
7 months ago
0.1.4
7 months ago
0.1.3
7 months ago
0.1.2
7 months ago
0.1.1
7 months ago
0.1.0
7 months ago