npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-handlebars

v1.2.2

Published

NestJS Handlebars module with i18n support

Downloads

42

Readme

NestJS Handlebars Template Engine

Description

This is a Handlebars template engine for NestJS. It uses the i18next library for internationalization.

Integration

To start using it, we first install the required dependencies. In this chapter we will demonstrate the use of the pagination for nestjs.

You simply need to install the package !

$ npm install --save nestjs-handlebars

Getting Started

Once the installation process is complete, we can import the HandlebarsModule into the root AppModule

import {Module} from '@nestjs/common';
import {HandlebarsModule} from 'nestjs-handlebars';
import {MailModule} from './modules/mail/mail.module';
import {SmsModule} from './modules/sms/sms.module';

@Module({
    imports: [
        HandlebarsModule.forRoot({
            // Global configuration
            i18n: {
                use: true,
                i18nHelperName: 'i18n',
            },
        }),
        MailModule,
        SmsModule,
    ],
})
export class ApplicationModule {
}

The forRoot() method supports all the configuration properties exposed by the handlebars constuctor . In addition, there are several extra configuration properties described below.

Configuration for I18nOptions

| Field | Description | |----------------------|---------------------------------------------------------------------------| | use | Indicates whether the I18n feature is enabled or not. | | i18nHelperName | Specifies a custom name for the Handlebars helper. | | directories | Array containing directories where translation files are located. | | defaultDirectoryName | Specifies the default directory name where translation files are located. |

Configuration for HandlebarsOptions

| Field | Description | |--------------------|----------------------------------------------------------------------| | partialDirectories | Array containing directories where partial files are located. | | templateOptions | An object of type RuntimeOptions used to determine template options. | | compileOptions | An object of type CompileOptions used to determine compile options. | | helpers | List of available Handlebars helpers. | | i18n | An object of type I18nOptions used to determine I18n features. |

Usage

HandlebarsModule.forSubModule()

The forSubModule() method is used to import the HandlebarsService into a sub-module. The first parameter is the module name and the second parameter is the configuration object. Configuration properties are the same as the * forRoot()* method and extra configuration property is templateDirectory. This property is used to specify the directory where the template files are located for the sub-module.

import {Module} from '@nestjs/common';
import {HandlebarsModule} from 'nestjs-handlebars';
import * as path from 'path';
import {SmsService} from './sms.service';

@Module({
    imports: [
        HandlebarsModule.forSubModule('sms', {
            templateDirectory: path.join(__dirname, 'templates'),
            partialDirectories: [path.join(__dirname, 'partials')],
            i18n: {
                use: true,
                directories: [path.join(__dirname, 'i18n')],
            },
        }),
    ],
    providers: [SmsService],
    exports: [SmsService],
})
export class SmsModule {
}

Service

Handlebars implements the Active Record pattern. With this pattern, you use model classes directly to interact with the database. To continue the example, we need at least one model. Let's define the Item Model.

import {Injectable} from '@nestjs/common';
import {HandlebarsService, InjectHandlebars} from 'nestjs-handlebars';

@Injectable()
export class SmsService {
    @InjectHandlebars('sms') private readonly handlebars: HandlebarsService;

    compileSmsTest() {
        return this.handlebars.compile('sms-test');
    }

    compileSmsWithLocale(locale?: string) {
        return this.handlebars.compile(`sms-with-locale`, {}, locale);
    }

    compileSmsWithPartial() {
        return this.handlebars.compile('sms-with-partial');
    }

    compileSmsWithPartialWithParam(param: { name: string }) {
        return this.handlebars.compile('sms-with-partial-with-param', param);
    }
}

Decorator

InjectHandlebars is a decorator that allows you to inject a HandlebarsService instance into a property. The decorator wants module name as a parameter.

License

nestjs-handlebars is MIT licensed.