0.1.32 • Published 4 months ago

@ackplus/nest-dynamic-templates v0.1.32

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

@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-templates

Quick Start

  1. 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 {}
  1. 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

4 months ago

0.1.31

4 months ago

0.1.30

4 months ago

0.1.29

4 months ago

0.1.28

4 months ago

0.1.27

4 months ago

0.1.26

4 months ago

0.1.25

4 months ago

0.1.24

4 months ago

0.1.23

4 months ago

0.1.22

4 months ago

0.1.21

4 months ago

0.1.20

4 months ago

0.1.19

4 months ago

0.1.18

4 months ago

0.1.17

5 months ago

0.1.16

5 months ago

0.1.15

5 months ago

0.1.14

5 months ago

0.1.13

5 months ago

0.1.12

5 months ago

0.1.10

5 months ago

0.1.9

5 months ago

0.1.8

5 months ago

0.1.7

5 months ago

0.1.6

5 months ago

0.1.5

5 months ago

0.1.4

5 months ago

0.1.3

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago