我可以将条件语句与 EJS 模板(在 JMVC 中)一起使用吗?

发布于 2024-12-17 05:14:42 字数 208 浏览 1 评论 0原文

如果是,语法是什么? 我的目标是当有多个评论时在“评论”一词前添加一个“s”。在 JMVC 应用程序的 jQuery.ejs 模板中。以下中断。我找不到任何条件文档...

<%=commentsNumber%> comment<% if (commentsNumber > 1) { %> s <% } %>

and if yes, what is the syntax?
My goal is to prepend an 's' to the word 'comment' when there is more than one. in an jQuery.ejs template in a JMVC app. The following breaks. I can't find any docs for conditionals...

<%=commentsNumber%> comment<% if (commentsNumber > 1) { %> s <% } %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

你げ笑在眉眼 2024-12-24 05:14:42

如果条件结构正确,则条件会起作用,我遇到了这个问题并解决了。

对于条件语句,else 之前的标记必须与前面的 if 的结束标记配对,否则语句将单独计算并产生错误。

错误!

<% if(true){ %>
   <h1>foo</h1>
<% } %>
<% else{ %>
   <h1>bar</h1>
 <% } %>

正确的

<% if(true){ %>
   <h1>foo</h1>
 <% } else{ %>  
   <h1>bar</h1>
<% } %>

希望这有帮助。

Conditionals work if they're structured correctly, I ran into this issue and figured it out.

For conditionals, the tag before else has to be paired with the end tag of the previous if otherwise the statements will evaluate separately and produce an error.

ERROR!

<% if(true){ %>
   <h1>foo</h1>
<% } %>
<% else{ %>
   <h1>bar</h1>
 <% } %>

Correct

<% if(true){ %>
   <h1>foo</h1>
 <% } else{ %>  
   <h1>bar</h1>
<% } %>

hope this helped.

风和你 2024-12-24 05:14:42

对于其他偶然发现这一点的人,您还可以在条件语句中使用 ejs params/props:

recipes.js 文件:

app.get("/recipes", function(req, res) {
    res.render("recipes.ejs", {
        recipes: recipes
    });
});

recipes.ejs 文件:

<%if (recipes.length > 0) { %>
// Do something with more than 1 recipe
<% } %>

For others that stumble on this, you can also use ejs params/props in conditional statements:

recipes.js File:

app.get("/recipes", function(req, res) {
    res.render("recipes.ejs", {
        recipes: recipes
    });
});

recipes.ejs File:

<%if (recipes.length > 0) { %>
// Do something with more than 1 recipe
<% } %>
鸩远一方 2024-12-24 05:14:42

是的,您可以在 EJS 中使用条件语句,例如 if else 、三元运算符甚至 switch case ,

例如

三元运算符
<%- 角色 == '管理员' ? '超级管理员':角色=='子管理员'? '子管理员':角色 %>

切换大小写

<% switch (role) {
case 'Admin' : %>
        Super Admin
        <% break;

case 'eventAdmin' : %>
        Event Admin
        <% break;

case 'subAdmin' : %>
        Sub Admin
        <% break;

} %>

Yes , You can use conditional statement with EJS like if else , ternary operator or even switch case also

For Example

Ternary operator :
<%- role == 'Admin' ? 'Super Admin' : role == 'subAdmin' ? 'Sub Admin' : role %>

Switch Case

<% switch (role) {
case 'Admin' : %>
        Super Admin
        <% break;

case 'eventAdmin' : %>
        Event Admin
        <% break;

case 'subAdmin' : %>
        Sub Admin
        <% break;

} %>
怀念你的温柔 2024-12-24 05:14:42

EJS 的行为似乎有所不同,具体取决于您是否使用 { } 表示法:

我已经检查过,并且按照您的预期评估了以下条件:

<%if (3==3) {%>  TEXT PRINTED  <%}%>
<%if (3==4) {%>  TEXT NOT PRINTED  <%}%>

而这个则没有:

<%if (3==3) %>  TEXT PRINTED  <% %>
<%if (3==4) %>  TEXT PRINTED  <% %>  

EJS seems to behave differently depending on whether you use { } notation or not:

I have checked and the following condition is evaluated as you would expect:

<%if (3==3) {%>  TEXT PRINTED  <%}%>
<%if (3==4) {%>  TEXT NOT PRINTED  <%}%>

while this one doesn't:

<%if (3==3) %>  TEXT PRINTED  <% %>
<%if (3==4) %>  TEXT PRINTED  <% %>  
顾北清歌寒 2024-12-24 05:14:42

您还可以使用 else if 语法:

<% if (x === 1) { %>
    <p>Hello world!</p>
<% } else if (x === 2) { %>
    <p>Hi earth!</p>
<% } else { %>
    <p>Hey terra!</p>
<% } %>

You can also use else if syntax:

<% if (x === 1) { %>
    <p>Hello world!</p>
<% } else if (x === 2) { %>
    <p>Hi earth!</p>
<% } else { %>
    <p>Hey terra!</p>
<% } %>
ˉ厌 2024-12-24 05:14:42

我知道这是一个有点晚的答案,

您可以在ejs中使用if和else语句,如下所示

<% if (something) { %>
   // Then do some operation
<% } else { %>
   // Then do some operation
<% } %>

但是我想强调的另一件事是,如果您以这种方式使用代码,

<% if (something) { %>
   // Then do some operation
<% } %>
<% else { %>
   // Then do some operation
<% } %>

它将产生错误。

希望这对某人有帮助

I know this is a little late answer,

you can use if and else statements in ejs as follows

<% if (something) { %>
   // Then do some operation
<% } else { %>
   // Then do some operation
<% } %>

But there is another thing I want to emphasize is that if you use the code this way,

<% if (something) { %>
   // Then do some operation
<% } %>
<% else { %>
   // Then do some operation
<% } %>

It will produce an error.

Hope this will help to someone

Saygoodbye 2024-12-24 05:14:42

一个简单且通用的规则
例如,要将 javascript(for 循环)嵌入到我们的 ejs 模板中,我们可以遵循以下规则:

  1. 首先按照通常在 javascript 中编写的方式在 ejs 模板中编写 javascript 代码。

  2. 将 <% 放在开头,%>在每个纯 JavaScript 行的末尾。

例如:

if (num%2===0) {
    <p> this is an even number</p>
} else { 
<p> this is an odd number</p>
}

所以现在我们必须将 ejs 语法放在每一行中:

 <% if (num%2===0) {%>
    <p> this is an even number</p>
<%} else { %>
<p> this is an odd number</p>
<%}%>

one simple and general rule
for embedding javascript for example (for loops) into our ejs template we can follow the following rule:

  1. first write your javascript code in the ejs template as you normally write in the javascript.

  2. put the <% in the start and %> in the end of every pure javascript line.

for example:

if (num%2===0) {
    <p> this is an even number</p>
} else { 
<p> this is an odd number</p>
}

so now we have to put the ejs syntax in every line :

 <% if (num%2===0) {%>
    <p> this is an even number</p>
<%} else { %>
<p> this is an odd number</p>
<%}%>
寄居者 2024-12-24 05:14:42

只需缩短代码即可使用 ES6 功能。相同的东西可以写成

app.get("/recipes", (req, res) => {
    res.render("recipes.ejs", {
        recipes
    });
}); 

并且 Templateate 可以渲染为相同!

<%if (recipes.length > 0) { %>
// Do something with more than 1 recipe
<% } %>

Just making code shorter you can use ES6 features. The same things can be written as

app.get("/recipes", (req, res) => {
    res.render("recipes.ejs", {
        recipes
    });
}); 

And the Templeate can be render as the same!

<%if (recipes.length > 0) { %>
// Do something with more than 1 recipe
<% } %>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文