【问题标题】:can I use partials in a for loop in ejs?我可以在 ejs 的 for 循环中使用部分吗?
【发布时间】: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 向下传递到部分?

【问题讨论】:

    标签: for-loop ejs partials


    【解决方案1】:
    <%for(let post of posts){%> 
        <%-include('_post',{post: post}); %>
    <%}%>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      相关资源
      最近更新 更多