Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 958
Gavish Kumar Avatar asked Nov 17 '25 18:11

Gavish Kumar


1 Answers

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

<%- cchoice =='dog' ? '<p>good choice<p>' : '<p>cool!</p>' %>
like image 71
Ankit Agarwal Avatar answered Nov 19 '25 08:11

Ankit Agarwal