Template engines in Express

 Template engines in Express: Template engines in Express allow you to dynamically generate HTML pages. You can use a variety of template engines, such as Handlebars, EJS, and Pug. Here's an example of how to use the Handlebars template engine in Express:

javascript


const express = require('express');

const exphbs = require('express-handlebars');

const app = express();


app.engine('handlebars', exphbs());

app.set('view engine', 'handlebars');


app.get('/', (req, res) => {

  const data = { name: 'John' };

  res.render('home', data);

});


app.listen(3000, () => {

  console.log('Server listening on port 3000');

});

In this example, the home.handlebars template will be rendered with the data object. The exphbs function creates an instance of the Handlebars template engine, which is then set as the view engine for the app using the app.set method.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...