Expressjs +句柄格式化date

我有这个对象

{ title: 'A TITLE', description: 'Lorem Ipsum...', _id: 50806930bbf612a858000013, __v: 0, screenings: [ { venue: 'Venue1', dates: [Object] }, { venue: 'Venue2', dates: [Object] } ] } 

从我的mongoDB出来。

dates是一个dates列表(DAH!)。

我的问题是现在我想让他们与服务器端的时间格式化。

这看起来应该是我应该使用胡子lambdas,但它似乎不可能使用我的堆栈( expressjs巩固把手 …)

有人解决了这个?

这就是我现在要做的

 Theatre.find({"screenings.dates":{$gte:lastSunday, $lte:nextSunday}}, function(err, entities){ res.render('index', { entities: entities, giveitatry: function(a) { return moment(a).format("MMM Do YY") } }); }); 

并在我的模板上,我有:

 {{#entities}} <div class="span3">{{#giveitatry}} {{dates.0.}} {{/giveitatry}}</div> {{/entities}} 

这是expressjs conf的一部分

 var moment = require('moment'); var express = require('express') , cons = require('consolidate') , name = 'mustache'; app.configure(function(){ app.set('view engine', 'hjs'); app.engine('.hjs', cons.mustache); ... 

是的,扩展仍然hjs因为我开始使用hogan,但我没有能够做到这一点,所以我移动巩固+胡子。

我正在使用节点v0.8.8和expressjs 3.0.0rc4

如果您使用mustache.js lambdas,则需要返回一个函数。 该函数的参数是text ,其中包含模板内容的文本表示和可以显式调用以呈现模板的渲染。

到目前为止,我发现的最佳解决scheme是首先使用默认的渲染器,然后将生成的datestring传递给新的Date对象的构造函数,然后再传递给moment函数。 最后,您只需以期望的格式返回date。

完整代码:

 giveitatry: function() { return function(text, render) { var date = moment(new Date(render(text))); return date.format("MMMM Do YY"); } } 

date列表的工作方式也很相似:

 {{#entities}} {{#dates}} <div class="span3">{{#giveitatry}}{{.}}{{/giveitatry}}</div> {{/dates}} {{/entities}} 

在这个问题上你并不孤单。 mustache.js的问题跟踪器(见[1] , [2] )正在进行帮助程序/filter的介绍,这将为这个问题提供一个更清晰的解决scheme。