Nest: Documentation Issue: How does one register helpers with handlebars?

Created on 30 May 2018  路  3Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[X] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Can't figure out how/where to register handlebars helpers.

Expected behavior

Should be able to figure out.

Minimal reproduction of the problem with instructions

Lookup how to register handelbars helpers in nestjs dependency injection model with project structure as described in the documentation.

What is the motivation / use case for changing the behavior?

So people will know how to do this.

Environment


Nest version: 5.0.0

For Tooling issues:

  • Node version: 8.9.1
  • Platform: Windows
question 馃檶

Most helpful comment

I think he was trying to add helpers to handlebars engine (as he wrote). In docs there is an example of using 'hbs' engine - leveraging list of engines compliant with express out of the box. So this question is not really about nestJS I think.

That being said, I myself used to have some troubles adding helpers to hbs engine. Finally I decided to use express handlebars. Using this package you just need to add object with helpers as part of config passed to function creating hbs instance, that is going to be used as engine in your app. I'll leave example, maybe somebody would use it.

import { NestFactory } from '@nestjs/core';
import { join } from 'path';
import * as exphbs from 'express-handlebars';

import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useStaticAssets(join(__dirname, 'public'));
  app.setBaseViewsDir(join(__dirname, 'views'));

  const helpers = {
    hlp: (echo) => `Echo: ${echo}.`,
  };
  const hbs = exphbs.create({
    defaultLayout: 'main',
    layoutsDir: join(__dirname, 'views', 'layouts'),
    helpers,
  });

  app.engine('handlebars', hbs.engine);
  app.setViewEngine('handlebars');

  await app.listen(process.env.PORT);
}
bootstrap();

All 3 comments

Could you share an example of the sample code to show us what actually you're trying to achieve?

I think he was trying to add helpers to handlebars engine (as he wrote). In docs there is an example of using 'hbs' engine - leveraging list of engines compliant with express out of the box. So this question is not really about nestJS I think.

That being said, I myself used to have some troubles adding helpers to hbs engine. Finally I decided to use express handlebars. Using this package you just need to add object with helpers as part of config passed to function creating hbs instance, that is going to be used as engine in your app. I'll leave example, maybe somebody would use it.

import { NestFactory } from '@nestjs/core';
import { join } from 'path';
import * as exphbs from 'express-handlebars';

import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useStaticAssets(join(__dirname, 'public'));
  app.setBaseViewsDir(join(__dirname, 'views'));

  const helpers = {
    hlp: (echo) => `Echo: ${echo}.`,
  };
  const hbs = exphbs.create({
    defaultLayout: 'main',
    layoutsDir: join(__dirname, 'views', 'layouts'),
    helpers,
  });

  app.engine('handlebars', hbs.engine);
  app.setViewEngine('handlebars');

  await app.listen(process.env.PORT);
}
bootstrap();

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

2233322 picture 2233322  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments

FranciZ picture FranciZ  路  3Comments

tronginc picture tronginc  路  3Comments