Gavish Kumar
Gavish Kumar

Reputation: 59

If-else shorthand in ejs template?

Below is the code from EJS file which i am rendering but i am not able figure out the correct syntax for the if statement shorthand.

Here "cchoice" is the object that i am passing while rendering and it is working if i remove the if statement, hence there is no problem in getting its value.

<h1>Welcome,
    <%= cchoice %>
</h1>
<% cchoice =='dog' ? <p>good choice</p> : <p>cool!</p> %>

The error i am getting is :

SyntaxError: Unexpected token < in /views/new.ejs while compiling ejs

Suggest the correct syntax for the above shorthand for if statement.

Upvotes: 2

Views: 2955

Answers (1)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You need to use <%- and surround the HTML with quote:

<%- cchoice =='dog' ? '<p>good choice<p>' : '<p>cool!</p>' %>

Upvotes: 8

Related Questions