This question already has an answer here:
I'm currently working on a simple web app for hosting games, just to mess around. I have some javascript/json files for handling HTTP requests and setting up variables within the rendered HTML pages. However, I'm running into a weird problem when trying to use if, else if, else statements in my EJS files. Specifically, I'm running into a syntax error stating there is an unexpected identifier while compiling ejs. Here is snippet of my code from the EJS file below:
<%
var friendsList = '';
if (current_user.username == 'Guest') {
friendsList += '<li>Login to see your friends online</li>';
}
else if(current_user.friends.length == 0) {
friendsList += '<li>You haven't made any friends yet</li>';
}
else {
for (i=0; i<current_user.friends.length; i++) {
friendsList += '<li>' + current_user.friends[i] + '</li>';
}
} %>
<%- friendsList %>
The idea is to buildup a javascript string that matches HTML formatting and then insert that string directly into the existing HTML in the EJS file. This code results in the error I described. However, if I remove the else-if part of the statement as shown below:
<%
var friendsList = '';
if (current_user.username == 'Guest') {
friendsList += '<li>Login to see your friends online</li>';
}
else {
for (i=0; i<current_user.friends.length; i++) {
friendsList += '<li>' + current_user.friends[i] + '</li>';
}
} %>
<%- friendsList %>
there is no error and the HTML renders the way I want it to, albeit with some missing functionality. I have wracked my brain and looked into why the code is behaving like this all day but cannot find a solution. Does anyone know why the EJS isn't compiling with the else-if statement included?
Aucun commentaire:
Enregistrer un commentaire