【发布时间】:2022-05-08 02:45:46
【问题描述】:
我正在尝试在 EJS 的 for 循环中包含部分内容。
<% let index = 1 %>
<% for (let item in db) { %>
<div class="box unfocused box_<%=index %> ">
<% if (db[item].type === "video") { %>
<%- include ('partials/_video.ejs') %>
<% } else if (db[item].type === "collection") { %>
<%- include ('partials/_collection.ejs') %>
<% } else if (db[item].type === "box") { %>
<%- include ('partials/_box.ejs') %>
<% } %>
</div>
<% index ++ %>
<% } %>
由于某种原因,在 for 循环中定义的变量没有被传递到部分中。例如,_video.ejs 看起来像这样:
<video muted=true autoplay=true loop=true width="250">
<source src="<%=db[item].src%>" type="video/mp4">
</video>
当我运行代码时,浏览器告诉我item 没有在_video.ejs 中定义(item 在 for 循环中定义)。
如何将item 向下传递到部分?
【问题讨论】: