I currently have my homepage showing the 6 oldest Stories posts from MongoDB
I use the below for the query from Mongo:
var query = Story
.find({maxlines:10})
.sort({_id:1})
.limit(6);
Then I'm using this to pass the information to dust.js
app.get('/', function(req, res){
query.exec(function(err, story) {
if (err) console.log(err);
res.render('index', {
title: 'Test!',
storytitle1: story[0].title,
storylines1: story[0].lines,
storyid1: story[0].sid,
storyslug1: slugs(story[0].title),
storytitle2: story[1].title,
storylines2: story[1].lines,
storyid2: story[1].sid,
storyslug2: slugs(story[1].title),
storytitle3: story[2].title,
storylines3: story[2].lines,
storyid3: story[2].sid,
storyslug3: slugs(story[2].title),
storytitle4: story[3].title,
storylines4: story[3].lines,
storyid4: story[3].sid,
storyslug4: slugs(story[3].title),
storytitle5: story[4].title,
storylines5: story[4].lines,
storyid5: story[4].sid,
storyslug5: slugs(story[4].title),
storytitle6: story[5].title,
storylines6: story[5].lines,
storyid6: story[5].sid,
storyslug6: slugs(story[5].title)
});
});
});
Then on i have the below on the .dust template
<h1 class="title">{storytitle1}</h1>
{#storylines1}
<ul>{text}</ul>
{/storylines1}
<h1 class="title">{storytitle2}</h1>
{#storylines2}
<ul>{text}</ul>
{/storylines2}
Basically for each of the 6 i render them individually, it works perfectly fine, but i'm wondering if there is a better/cleaner way to do this? Because i want to implement another page '/all/ which shows for example the last 20 stories and i don't want to write them all individually within the template.
Try this.
Code:
app.get('/', function(req, res){
query.exec(function(err, story) {
if (err) console.log(err);
for (var i = 0; i < story.length; i++) {
story[i].slug = slugs(story[i].title);
}
res.render('index', {
title: 'Test!',
stories: story
});
});
});
Template:
{#stories}
<h1 class="title">{title}</h1>
{#lines}
<ul>{text}</ul>
{/lines}
{/stories}