简体   繁体   English

如何在 EJS 模板中显示这个?

[英]How can I display in an EJS template this?

I made a page where I get from database some articles.我做了一个页面,从数据库中获取一些文章。 With the help of underscore and _.groupBy I grouped them by years ( a field in the database ), now they are grouped like this在下划线和 _.groupBy 的帮助下,我按年份(数据库中的一个字段)对它们进行了分组,现在它们是这样分组的

 { '2001':
   [ { _id: 5e286db8a47af74400f4a4f7,
       title: 'Trying to connect to SSH, Permision Denied (public key)',
       typeofdisaster: 'Tsunami',
       year: '2001',
       month: 'December',
       day: '08',
       description: 'ADFASDFASD',
       aditional: 'fdasfas',
       image1:
        'images/2020-01-22T15:43:52.720Z-67287305_1471150326359475_6265289848051990528_o.jpg',
       image2: 'none',
       location: 'Scenic Highhwat 23412',
       postid: 'GyPUk',
       datePosted: '1579707832851',
       __v: 0 } ],
  '2009':
   [ { _id: 5e285af6f4caf441cc9b451f,
       title: 'This is an article',
       typeofdisaster: 'Tsunami',
       year: '2009',
       month: 'March',
       day: '5',
       description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
       aditional: 'https://www.google.com',
       image1:
        'images/2020-01-22T14:23:50.840Z-67287305_1471150326359475_6265289848051990528_o (1).jpg',
       image2: 'none',
       location: '245 Scenic Highway Lawrenceville, GA',
       postid: 'v6Lih',
       datePosted: '1579703030971',
       __v: 0 },
     { _id: 5e286a5ffeadf743747f7ff6,
       title: 'This is a post',
       typeofdisaster: 'Tornado',
       year: '2009',
       month: 'December',
       day: '08',
       description: 'LOREM IPSUM',
       aditional: 'https://www.google.com',
       image1:
        'images/2020-01-22T15:29:34.965Z-82668748_468933630458447_4082761341385834496_n.jpg',
       image2: 'none',
       location: 'Vasile Alecsandri 16, Campia Turzii, Cluj',
       postid: 'r39GP',
       datePosted: '1579706975104',
       __v: 0 } ] }

All good.都好。 They are grouped well, but how can I render this in an EJS File?它们分组得很好,但是如何在 EJS 文件中呈现它? I did this because I want to make an archive of posts sorted by years like this我这样做是因为我想制作一个像这样按年份排序的帖子存档

 2009 
    Post Title
    Post Title 2 
 2005
    Post Title
 2001 
    Post title 
    Post title 2
    Post title 3

this is the function that renders that page这是呈现该页面的函数

exports.getIndex = async (req,res,next) => {

    const authenticated = req.session.isLoggedIn;
    let postLists = [];
      let posts = await Post.find();
      for(let i = 0; i< posts.length;i++) {
        postLists.push(posts[i]);
      }

     console.log(_.groupBy(postLists, 'year'));

res.render('index', {
      pageTitle:'DISASTER DATABASE |  HOME',
      path:'/',
      authenticated: authenticated,
      user: req.session.user,
      posts: _.groupBy(postLists,'year')
    });

I'm really bad at Front End and I don't know how to implement in the EJS file this thing, I mean I got all data I need but I don't know how to display it.我在前端真的很糟糕,我不知道如何在 EJS 文件中实现这个东西,我的意思是我得到了我需要的所有数据,但我不知道如何显示它。

<%- include('./includes/head.ejs'); %>
<main>
 <%- include('./includes/navigation.ejs'); %>

 <div class="split c-archivecontainer">
  <h5 style=" color: #5D4954;
  text-transform: uppercase;
  letter-spacing: 5px;
  font-size:15px;
  font-family: 'Poppins', sans-serif;">Disasters archive</h5>
 </div>
 <div class="split c-mapcontainer">
        <h5 style=" color: #5D4954;
        text-transform: uppercase;
        letter-spacing: 5px;
        font-size:15px;
        font-family: 'Poppins', sans-serif;">Disasters Map</h5>
 </div>

 <script>


 </script>
</main>
<%- include('./includes/end.ejs'); %>

Can you please help me implement that, I mean how to loop through the all years display years then the posts assigned to that year.你能帮我实现这个吗,我的意思是如何遍历所有年份显示年份然后分配给该年份的帖子。 Thank you in advance!先感谢您!

Ignore the second div I made that just for splitting the page, in the first div I want to put the archive忽略我为拆分页面而制作的第二个 div,在我想放置存档的第一个 div 中

You can write your code like this你可以这样写你的代码

<% array.forEach((value,key)) { %>

    <h3><%= key%></h3>

    <% for(let elem of value) { %>

    <span><%= elem.title%></span>

    <%}%>

<%}%>

Here you can run a forEach loop on your original array, which gives access you to key & value of every element在这里,您可以在原始数组上运行 forEach 循环,从而可以访问每个元素的键和值

Then, you can print key of the element & loop again through value of each element which is also a loop然后,您可以打印元素的键并通过每个元素的值再次循环,这也是一个循环

inside this second loop, you can print title of each post在第二个循环中,您可以打印每个帖子的标题

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

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