Inline Ternary Operator In Nunjucks And EJS
Nunjucks
Nunjucks Inline Ternary Operator examples:
<span>{{ post.data.uuid if post.data.uuid else absolutePostUrl }}</span>
it check the
post.data.uuid
is not undefined otherwise returnabsolutePostUrl
EJS
EJS Inline Ternary Operator examples:
You need to replace the <% %>
tag with the <%= %>
tag in order to output the expression value:
<li class="<%= currentMenu === 'dashboard' ? 'active' : '' %>">
<!-- -->
</li>
As the EJS documentation states, the <% %>
tags are for control-flow, no output code; whereas the <%= %>
tags output and interpolate the value into the HTML template.
For example, the if
statement below uses <% %>
tags because the statement doesn’t need to be outputted into the HTML. Then inside of the condition, the variable is outputted and interpolated into the HTML template by using the <%= %>
tags: <%= currentMenu %>
.
<% if (currentMenu === 'dashboard') { %>
<span><%= currentMenu %></span>
<% } %>