Reputation: 384
I'm trying to use if/else
in ejs but whenever I add the else
part an error shows up.
<% if (user) {%>
<%=user.twitter.username%>
<%}%>
<%=else{%>
Hi hi
<%}%>
The error says: SyntaxError: Unexpected token else in C:\Users\Adetona\Dropbox\meme.africa\views\pages\index.ejs while compiling ejs
Whenever I removed the else
part everything works well.
What could I be doing wrong?
Upvotes: 2
Views: 670
Reputation: 1940
The ejs view engine needs to understand that it's compiling an if-else block so try writing it as shown below
Place the curly braces as shown below and tag it appropriately.
if {
// if block code statements
} else {
//else block code statements
}
so it becomes
<% if { %>
// if block code statements
<% } else { %>
//else block code statements
<% } %>
Upvotes: 0
Reputation:
<% %>
means that you are making an expression or declaring a variable etc...
<%- %>
the text will be printed but not escaped you could still do calculation inside like (a > b) ? a : b;
<%= %>
will escape html lets say you are getting a user input and print it on the screen then you want that to be escaped.
Upvotes: 1
Reputation: 7654
I believe you need to pair the else
with the end of the previous conditional block, so:
<% if (user) { %>
...
<% } else { %>
...
<% } %>
Upvotes: 3