Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 678
pslaw Avatar asked Aug 14 '18 16:08

pslaw


People also ask

How do I use handlebars in node JS?

HandleBars can be used to render web pages to the client side from data on the server-side. To use handlebars in express, we need to store HTML code into a . hbs extension in the 'views' folder in the source directory as hbs looks for the pages in the views folder. Now, we need to change the default view engine.

What design pattern does NestJS use?

NestJS Application Using CQRS Design Pattern.

Is Nest JS worth learning?

Nest. JS helps build lightweight, well-structured and amazing microservices and helps evolve the technology stack. The microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications. Out-of-the-box tools and features make development, extension, and maintenance efficient.


1 Answers

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.

like image 105
Kim Kern Avatar answered Oct 01 '22 18:10

Kim Kern