Joseph Chambers
Joseph Chambers

Reputation: 4068

res is not defined in ejs

Objective: I need to display different templates based on route. In my case it's /edit /new, and I need a condition to display tabs or something else.

In my router.get('/edit' cb) I tested this: res.url.includes('new') which returned true. I went inside my header.ejs and ran a condition:

<% if(res.url.includes('new')) { %>
  <h1>Something else</h1>
<% } else { %>
  <h1>This</h1>
<% } %>

However, it's telling me that res is not defined. My question is this: whats the best way to do this? My first instinct is to just create another header file but that's going to duplicate a lot of code but I may be over optimizing it.

Upvotes: 0

Views: 1846

Answers (1)

Sagi_Avinash_Varma
Sagi_Avinash_Varma

Reputation: 1509

you need to do res.render('template.ejs', { res: res });

check if u forgot to pass the { res: res }

Upvotes: 4

Related Questions