Convert Jade to EJS

大兔子大兔子 提交于 2019-12-21 21:15:06

问题


Can anyone please help by convert this Jade to EJS?

extends layout

block content
    h1.
        User List
    ul
        each user, i in userlist
            li
                a(href="mailto:#{user.email}")= user.username

回答1:


There is no block but an include logic available in EJS. Split up the "main layout" that way that you can include a header and footer (or whatever suits your needs). The iteration is expressed in plain JavaScript escaped in sequences of <% ... %>. Using <%= ... %> directly outputs the referenced var. Your resulting EJS code might look as follows:

<h1>User List</h1>
<ul>
    <% for (var i = 0; i < user.length; i++) { %>
        <li><a href="mailto:<%= user[i].email %>"><%= user[i].username %></a></li>
    <% } %>
</ul>

or, alternatively:

<h1>User List</h1>
<ul>
    <% user.forEach(function(user) { %>
        <li><a href="mailto:<%= user.email %>"><%= user.username %></a></li>
    <% )} %>
</ul>

The include syntax is straight forward:

<% include partials/header %>
...
<% include partials/footer %>

Note: Include files are located relative to the template with the include statement. Extensions are added automatically. So if your template is stored in let's say /views, the full path for the header include would be /views/partials/header.ejs.




回答2:


Turns out I needed a "For In" loop. Here is what worked for me after tweaking:

<h1>User List</h1>
  <ul>
    <% for (var i in userlist){%>
      <li><a href="mailto:<%= userlist[i].email %>"><%= userlist[i].username %></a></li>
    <%} %>

  </ul>


来源:https://stackoverflow.com/questions/23924107/convert-jade-to-ejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!