Kumar Kumar - 2 years ago 219
Node.js Question

How to use if condition in ejs template

Server Code

Details['11'] = {
'id': '11',
'name': 'Avanish'
res.render('/index',{'Message': Details});

Page code

var ERRORCONST = 'Error';
var NORECORDS = 'No records';

<%if(Message != undefined && Message != NORECORDS && Message != ERRORCONST ){%>
<span><%= key.id %> - <%= key.name %></span>

Getting error
NORECORDS is not defined
and print all code on page.
I have two error. One is how to use Multiple condition in single if statement and second is how to compare
varibale with server variable

Answer Source
        <% var ERRORCONST = 'Error';
           var NORECORDS = 'No records'; %>

In ejs template please use constant like this.

You can not compare ejs variable and local script variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download