Nunjucks have a lot build in filters, they do great help to writing templates.
But if we want more flexibility from native javascript language, how to do it with Nunjucks?
EJS and JSP have such feature.
For example, I want to show current date. I can't do it with something like {{ Date.now() }}
You always have at least 4 options:
You can set in your data something like:
coffee
data:
currentYear: new Date().getFullYear()
After passing into template call it with {{ currentYear }}
Your example doesn't work, because Data() is JS constructor, not global variable. To call constructor you need to define it with new Date(). new is one of those JS native operators that won't work in Nunjucks templates, thus you won't be able to call constructors right in your templates unless you'll added special filter or global function for it.
In your case you need anyway to map that constructor to filter, global variable or pass it as data.
If you want that you just need to use EJS. Nunjucks is not JS, even if it looks like a subset sometimes. Thanks for the detailed reply @ArmorDarks !
Most helpful comment
You always have at least 4 options:
You can set in your data something like:
coffee data: currentYear: new Date().getFullYear()After passing into template call it with
{{ currentYear }}Your example doesn't work, because
Data()is JS constructor, not global variable. To call constructor you need to define it withnew Date().newis one of those JS native operators that won't work in Nunjucks templates, thus you won't be able to call constructors right in your templates unless you'll added special filter or global function for it.In your case you need anyway to map that constructor to filter, global variable or pass it as data.