142

and if yes, what is the syntax? My goal is to prepend an 's' to the word 'comment' when there is more than one. in an jQuery.ejs template in a JMVC app. The following breaks. I can't find any docs for conditionals...

<%=commentsNumber%> comment<% if (commentsNumber > 1) { %> s <% } %>
Regis Zaleman
  • 3,182
  • 6
  • 29
  • 30

8 Answers8

239

Conditionals work if they're structured correctly, I ran into this issue and figured it out.

For conditionals, the tag before else has to be paired with the end tag of the previous if otherwise the statements will evaluate separately and produce an error.

ERROR!

<% if(true){ %>
   <h1>foo</h1>
<% } %>
<% else{ %>
   <h1>bar</h1>
 <% } %>

Correct

<% if(true){ %>
   <h1>foo</h1>
 <% } else{ %>  
   <h1>bar</h1>
<% } %>

hope this helped.

StanleyZheng
  • 4,008
  • 3
  • 21
  • 24
188

For others that stumble on this, you can also use ejs params/props in conditional statements:

recipes.js File:

app.get("/recipes", function(req, res) {
    res.render("recipes.ejs", {
        recipes: recipes
    });
});

recipes.ejs File:

<%if (recipes.length > 0) { %>
// Do something with more than 1 recipe
<% } %>
Jbird
  • 2,839
  • 1
  • 21
  • 28
  • 4
    Can you, in the case of an `include` statement, write the conditional inline? That is writing `<% if (true) { include foo/bar } %>` appears to error. Is there a method similar or is it necessary to break out the `include` by `<% %>`. – kuanb Sep 12 '16 at 21:48
  • Thanks, I was searching for this answer before trying it out. – CTala Jul 03 '18 at 23:21
  • Thank you! I was trying to display conditional elements from changes on the server. Your answer showed me that I needed to do have evaluate everything on the client. – buildpax Mar 11 '19 at 01:39
36

Yes , You can use conditional statement with EJS like if else , ternary operator or even switch case also

For Example

Ternary operator : <%- role == 'Admin' ? 'Super Admin' : role == 'subAdmin' ? 'Sub Admin' : role %>

Switch Case

<% switch (role) {
case 'Admin' : %>
        Super Admin
        <% break;

case 'eventAdmin' : %>
        Event Admin
        <% break;

case 'subAdmin' : %>
        Sub Admin
        <% break;

} %>
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
Om Prakash Sharma
  • 1,682
  • 22
  • 13
24

EJS seems to behave differently depending on whether you use { } notation or not:

I have checked and the following condition is evaluated as you would expect:

<%if (3==3) {%>  TEXT PRINTED  <%}%>
<%if (3==4) {%>  TEXT NOT PRINTED  <%}%>

while this one doesn't:

<%if (3==3) %>  TEXT PRINTED  <% %>
<%if (3==4) %>  TEXT PRINTED  <% %>  
Kristóf Aczél
  • 257
  • 2
  • 3
21

You can also use else if syntax:

<% if (x === 1) { %>
    <p>Hello world!</p>
<% } else if (x === 2) { %>
    <p>Hi earth!</p>
<% } else { %>
    <p>Hey terra!</p>
<% } %>
pzrq
  • 1,626
  • 1
  • 18
  • 24
  • 4
    what you need to highlight here is that the closing bracket has to **most definitively** be used in the same line as else/else if – Paulo Aug 23 '17 at 08:17
8

I know this is a little late answer,

you can use if and else statements in ejs as follows

<% if (something) { %>
   // Then do some operation
<% } else { %>
   // Then do some operation
<% } %>

But there is another thing I want to emphasize is that if you use the code this way,

<% if (something) { %>
   // Then do some operation
<% } %>
<% else { %>
   // Then do some operation
<% } %>

It will produce an error.

Hope this will help to someone

Achintha Isuru
  • 2,662
  • 18
  • 24
3

one simple and general rule for embedding javascript for example (for loops) into our ejs template we can follow the following rule:

  1. first write your javascript code in the ejs template as you normally write in the javascript.

  2. put the <% in the start and %> in the end of every pure javascript line.

for example:

if (num%2===0) {
    <p> this is an even number</p>
} else { 
<p> this is an odd number</p>
}

so now we have to put the ejs syntax in every line :

 <% if (num%2===0) {%>
    <p> this is an even number</p>
<%} else { %>
<p> this is an odd number</p>
<%}%>
2

Just making code shorter you can use ES6 features. The same things can be written as

app.get("/recipes", (req, res) => {
    res.render("recipes.ejs", {
        recipes
    });
}); 

And the Templeate can be render as the same!

<%if (recipes.length > 0) { %>
// Do something with more than 1 recipe
<% } %>

xetryDcoder
  • 151
  • 1
  • 7