Heyrio
Heyrio

Reputation: 129

How do I render multiple partials based off of JSON data?

I have a database with user data, If I wanted to render a partial for each document in my MongoDB collection how would I do that?

I can render one partial by just adding it to my view but how do I make it render multiple ones automatically without me retyping this {{>somePartial}} for each new document added?

I looked around and found something suggesting AJAX they were not completely similar to my problem. If this is the solution is there another way other then AJAX?

every time a new document is added to MongoDB I'd like a new partial added to my view (I don't mind if it only shows when the page is refreshed)

{{>somePartial}} // Different data
{{>somePartial}} // Different data
{{>somePartial}} // Different data

Upvotes: 2

Views: 1493

Answers (1)

Dev Singh
Dev Singh

Reputation: 67

You can make an array of all the data from your monogdb then you can render your page and pass your data to the ejs template and add dynamic includes and pass different data e.g.

<% for (let i = 1; i <= <length>; i++) { %>
    <%- include("<partial>",{data:"your_data_for_this_index"}) %>
<% } %>

Upvotes: 1

Related Questions