Making use of utility libraries in server-side Jade templates
Jade is a clean and powerful template engine well known to Node.js developers, but it lacks advanced utility methods (such as to manipulate or format strings, dates, etc.).
Luckily, libraries such as underscore.js, underscore.string and moment.js provide a wealth of such methods one could use when generating server-side views.
Here's one way to do it in a standard Express.js application, with the help of app.locals:
Add these lines in your app entry point (usually app.js):
express = require('express');
...
app = express();
app.locals._ = require('underscore');
app.locals._.str = require('underscore.string');
app.locals.moment = require('moment');
Then, since you can use plain JavaScript code in in your Jade views, you can easily do:
span.price #{_.str.numberFormat(product.price, 2)} EUR
...
span.created-at= moment(product.createdAt).format('DD.MM.YYYY')
That's it!
Written by Ionut-Cristian Florescu
Related protips
7 Responses
Nice! Very clever approach. Now I can get rid of my virtual properties in mongoose. Thanks for sharing!
Thanks for reading, @third!
Very Helpful, and Powerful! I am now the Master of all DATES!!!
And don't even get me started with Currencies, I will conquer them as well. No formatting shall disrespect me from this day forward.
@jensanity5000 :-) Thanks for reading, Adam!
Yes, displaying date/time/numbers in human-readable formats is one of the possible benefits. I also use the same approach for stuff like capitalization, building sentences from arrays, etc.
You can even define your own "formatting" library and make it available to jade templates in the same way for more complex tasks...
@icflorescu
Question for you, is the only thing stopping me from loading up all kinds of stuff to locals performance? any idea on the performance impact of adding libraries which you may or may not use?
As you probably know already, in Node.js modules are cached. So if you require
the same module in multiple files across your application, it won't get loaded multiple times and it won't take more memory.
However, I believe it's always a good practice - and I try to stick to it - to only require the modules you need. Adding libraries which you "may or may not use" will certainly increase the memory footprint and deployment size. I mean, Node.js apps have the potential to be fast and lean, but one could easily turn them into sluggish and bulky by require
ing the whole npm registry :-)
For instance, I was using mysql
, cli-color
and commander
during development/deployment and legacy-data importing phase in a recent project. Those modules weren't necessary in production, so I added them in devDevelopment
section of package.json
, but they were still being deployed to Heroku, so I had to specifically add them to .slugignore
. As a result, the slug size went down from 20 MB to 16 MB.
So I'd say there's no generic answer to your question; the only way to actually know is testing...
@icflorescu
Thanks for the excellent answer!