1
votes

i tried to switch condition inside the ejs template but i got an error

here's my code :

<% switch(check) {%>
    <% case check == 'like' : %>
        <button href="#<%=item._id%>" id="like"  class="recommendation good  open-recommendation-form">
            <span class="icon-like"></span>
        </button>
        <button href="#<%=item._id%>" id="dislike"  class="recommendation bad hoverable open-recommendation-form">
            <span class="icon-dislike"></span>
        </button>
        <%break%>
    <% case check == 'dislike' : %>
        <button href="#<%=item._id%>" id="like"  class="recommendation good hoverable open-recommendation-form">
            <span class="icon-like"></span>
        </button>
        <button href="#<%=item._id%>" id="dislike"  class="recommendation bad  open-recommendation-form">
            <span class="icon-dislike"></span>
        </button>
        <%break%>
    <% default : %>
        <button href="#<%=item._id%>" id="like"  class="recommendation good hoverable open-recommendation-form">
            <span class="icon-like"></span>
        </button>
        <button href="#<%=item._id%>" id="dislike"  class="recommendation bad hoverable  open-recommendation-form">
            <span class="icon-dislike"></span>
        </button>  
    <%break%>  
<%} %> 

and the error i got :

SyntaxError: Unexpected token ';' in /Users/ismail/Desktop/expressjs-blank/views/dashboard/purchases.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
    at new Function (<anonymous>)
    at Template.compile (/Users/ismail/Desktop/expressjs-blank/node_modules/ejs/lib/ejs.js:632:12)
    at Object.compile (/Users/ismail/Desktop/expressjs-blank/node_modules/ejs/lib/ejs.js:368:16)
    at handleCache (/Users/ismail/Desktop/expressjs-blank/node_modules/ejs/lib/ejs.js:216:18)
    at tryHandleCache (/Users/ismail/Desktop/expressjs-blank/node_modules/ejs/lib/ejs.js:255:16)
    at View.exports.renderFile [as engine] (/Users/ismail/Desktop/expressjs-blank/node_modules/ejs/lib/ejs.js:461:10)
    at View.render (/Users/ismail/Desktop/expressjs-blank/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users/ismail/Desktop/expressjs-blank/node_modules/express/lib/application.js:640:10)
    at Function.render (/Users/ismail/Desktop/expressjs-blank/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/ismail/Desktop/expressjs-blank/node_modules/express/lib/response.js:1012:7)
1
The code you shared is not related to the error you are getting. The symbol mentioned in the error isn't in your code anywhere. - GrumpyCrouton

1 Answers

0
votes

Changing the switch/case statements to the following should fix the issue (simplified version with only one case):

<% switch(check) {
case 'like' : %>
<button href="#<%= item._id %>" id="like" class="recommendation good  open-recommendation-form">
    <span class="icon-like">TEST</span>
</button>
<% break;
}
%>

Rendering the page with the following, should definitely work:

res.render('name-of-ejs-template', {item: {_id: "https://www.stackoverflow.com"}, check: 'like'});