Asked 7 years ago
9 Feb 2017
Views 1508
samir

samir posted

what is view engine in express framework ?

how to use view engine in express framework ?
what is view engine in express framework ?
how one can configure or use it , is it suitable to use , what is the best way to use the view engine in express framework ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

view engine used for template in express framework ,
differ type of view engine available in the express framework ,


1) ejs
how EJS work in node JS Express framework
install ejs


npm i ejs


use it with your express framework project

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



make a folder named " views " in your project
declare "views" folder named as view folder

app.set('views',path.join (__dirname , 'view'));


make the view file named "index.ejs" in views folder
index.ejs

 <html>
<head>
<link rel="stylesheet" href="bower_components/boostrap/dist/css/boostrap.css"/>
</head>
<body>
<div class="container">
        <h1><%= title %></h1>
   </div>
 </body>
</html>


load view index.ejs by render method

app.get("/",function(req,res){
    res.render('index')
});


2. jade
how jade work in node JS - Express framework
install jade


npm i jade


use it with your express framework project

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



make a folder named " views " in your project
declare "views" folder named as view folder

app.set('views',path.join (__dirname , 'view'));


make the view file named "index.jade" in views folder
index.ejs

 doctype html(xmlns='http://www.w3.org/1999/xhtml', lang='en-GB', xml:lang='en-GB')
head
    link(rel='icon', href='favicon.ico')
    |     
    title  
  body
    #background.container-fluid



load view index.ejs by render method

app.get("/",function(req,res){
    res.render('index')
});

Rasi

Rasi
answered Nov 30 '-1 00:00

one can use HTML as template also with express framework at NODE JS

how to use HTML for template for Express Framework with Node JS

for the HTML parser we can use hogan , JavaScript templating from Twitter.

for hogan you need consolidate module.

lets install it consolidate and hogan module.

install consolidate


npm i consolidate




now you can start code.

how to load hogan in express framework ?


var app=express().set('view engine', 'html')
.engine('html', consolidate.hogan);


1. initiate express

var app=express();


2. set view Engine

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



3. load hogan

app.engine('html',consolidate.hogan);




create view HTML at views folder

<html>
<head>
 </head>
<body>
<div class="container">
        <h1>
{{title}}
        </h1>
</div>
  </body>
</html>


check github for more code
Post Answer