ejs 模板中的 if-else 简写?

Gav*_*mar 2 javascript if-statement ejs conditional-statements express

下面是我正在渲染的 EJS 文件中的代码,但我无法找出 if 语句简写的正确语法。

这里的“cchoice”是我在渲染时传递的对象,如果我删除 if 语句它就可以工作,因此获取它的值没有问题。

<h1>Welcome,
    <%= cchoice %>
</h1>
<% cchoice =='dog' ? <p>good choice</p> : <p>cool!</p> %>
Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

SyntaxError: 编译 ejs 时 /views/new.ejs 中出现意外的标记 <

为上述 if 语句的简写提出正确的语法建议。

Ank*_*wal 8

您需要使用<%-HTML 并将其括起来:

<%- cchoice =='dog' ? '<p>good choice<p>' : '<p>cool!</p>' %>
Run Code Online (Sandbox Code Playgroud)