Pradip Dhakal
Pradip Dhakal

Reputation: 1962

How to use if statement in ejs with strings?

I have a page that - If demo.text is 'disabled' than disabled demo button else enabled demo button.

index.ejs

<% for(project of projects) { %>
   <a class="info" href="#"> 
<% if(project.demo == 'disabled') { %>
   <button class="btn btn-primary" disabled> Button1 </button>
<% } else { %>
  <button class="btn btn-primary"> Button1 </button>
 </a>
<% } %>`

app.js

app.get('/', (req, res) => {
res.render('index', {
    projects: [
        { demo: 'disabled' },
        { demo: '' }
    ]
  });
});

Upvotes: 2

Views: 6191

Answers (1)

Kalaiselvan
Kalaiselvan

Reputation: 2134

while using EJS Template engine opening and closing should be correct. for further references http://ejs.co/#docs

 <% for(project of projects) { %>
      <a class="info" href="#"> 
         <% if(project.demo == 'disabled') { %>
              <button class="btn btn-primary" disabled> Button1 </button>
         <% } else { %>
             <button class="btn btn-primary"> Button1 </button>
         <% } %>
     </a>
<%}%>

Upvotes: 4

Related Questions