简体   繁体   中英

How to set up a default layout in nest.js using Handlebars.js?

I'm just starting with nest.js having some previous experience with node and express.

I'm trying to set up a default layout for my partials and response renders. Here's my main.ts :

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as hbs from 'hbs';

async function bootstrap() {
    const app = await NestFactory.create(AppModule);

    app.setBaseViewsDir(__dirname + '/views');
    hbs.registerPartials(__dirname + '/views/partials');
    app.setViewEngine('hbs');

    await app.listen(3000);
}
bootstrap();

I have a feeling that there should be a default layout defined somewhere in that code but I have no idea how to proceed. My Google-fu has failed me, so can anyone give me a hand with this?

You can use

app.set('view options', { layout: 'index' });

You still need to use @Render('valid-layout') on all controllers and the provided layout name must be valid. It will just be overwritten by your default layout.

This worked on a NestExpressApplication:

Import handlebars:

import * as hbs from 'hbs';
import { join } from 'path';

setting hbs as viewEngine a

app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('hbs');
hbs.registerPartials(join(__dirname, '..', '/views/partials'));

Worked also with helpers:

hbs.handlebars.helpers = {
...require('handlebars-helpers')()
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM