简体   繁体   English

使用来自 EJS in 和 EJS if 语句的变量

[英]Using variable from EJS in and EJS if statement

Basically, I can't figure out how to use an EJS "variable" (don't know the real name for it. In my case it's <%=postContent[i]%> ) within an EJS if statement as shown below.基本上,我无法弄清楚如何在 EJS if 语句中使用 EJS“变量”(不知道它的真实名称。在我的情况下它是<%=postContent[i]%> ),如下所示。

<% for (i = 0; i <= 10; i++) { %>
    <% if (<%-postContent[i]%>.includes('gyfcat')) { %>
        <div>PLACEHOLDER</div>
    <% } else { %>
        <img src="<%=postContent[i]%>" class="redditImages">
    <% } %>
<% } %>

I basically want it so when the string "gyfcat" is in the postContent string (it's a URL), the first block of code runs, else, runs the <img> code.我基本上想要它,所以当字符串"gyfcat"postContent字符串(它是一个 URL)中时,第一个代码块运行,否则运行<img>代码。

The error I'm getting is Error: Could not find matching close tag for "<%".我得到的Error: Could not find matching close tag for "<%".Error: Could not find matching close tag for "<%".

只需删除postContent[i]周围的<%=%> postContent[i]解决我的问题。

<div> 
    <% if(typeof datas !='undefined') { %>
    <% datas.forEach(function(data) { %>
       <div> Id:<%= data.id %></div>
       <div> Title:<%= data.title %></div>
       <div> Body:<%= data.body%></d>
    <% }); %> 
    <% } %>
 </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM