I am in the process of learning Node.js/Express.js, and as an experiment I am attempting to build a small blog application.
I render the page, passing the articles:
response.render('index.jade', {
title: 'Blog'
, locals: {
articles: articles
}
});
Here's my Jade template for that:
h1= title
section
h1 Articles
ul
each article in articles
li
a(href='/article/' + article.slug)= article.title
span at #{article.created_at}
This displays a list of articles with the title and date. However, I need a way of formatting the date into a more readable format. I have discovered libraries that allow you to do this, but my real question is how I should go about integrating this into my template? I can either pass the moment module to the template and format the date inline there, or I can format it in the route and add it onto the article
object. How would you do this?
I have the following helpers in my application:
date: function (date) {
return moment(date).format('YYYY/MM/DD HH:mm:ss');
},
fromNow: function(date) {
return moment(date).fromNow();
}
This could depend on your application but I didn't see the need of using different date formats all over my site.
I would go with adding to the template if you want less and clean code. If you want performance, format it in the route itself.