如何更改空格键输出的显示?
How to alter the display of what Spacebars outputs?
所以我在模板中有这样的东西:
{{#each posts}}
<li>{{date}}</li>
{{/each}}
这显示正常,但问题是我的 "date" 变量显示为 Sat Feb 07 2015 19:47:13 GMT-0800 (PST)
,这是准确的,但有点长且不必要。
我希望日期看起来更简单,例如 February 7, 2015
。但是,请原谅我的不理解,但我知道如何做到这一点的唯一方法是开始时以漂亮的格式存储它,但如果我必须这样做,这似乎效率不高每次我想让东西看起来有点不同。
理想情况下,我想马上将它包装起来 () 并生成如下内容:
moment(date).format("MMMM Do YYYY")
我试着改用这个,但它不起作用:
{{#each posts}}
<li>{{moment(date).format("MMMM Do YYYY")}}</li>
{{/each}}
试试这个。
Template.example.rendered = function(){
//here we are getting a new date
var d = moment(new Date());
//here we take the li element and we set it on the html
document.getElementById("dateFormated").innerHTML = d.format('LL');
}
<template name="example">
<li id="dateFormated"> </li>
</template>
更新
因为您使用的是 {{#each}} 迭代器
你可以做到。
var d = moment(new Date()).format('LL');
或
var d = moment(new Date());
dFormat = d.format('LL');
在插入时,插入一个像Posts.insert({submitted:dFormat})
这样的字段
并且在 {{each}}
通话中,就像您
{{#each posts}}
<li>{{subbmited}}</li>
{{/each}}
不插入数据库(另一种选择)
Template.example.helpers({
formattedData:function(){
return moment(new Date()).format('LL');
}
})
{{#each posts}}
<li>{{formattedData}}</li>
{{/each}}
添加助手
Template.yourTemplate.helpers({
formattedDate: function(){
return moment(this.date).format("MM/DD/YYYY"); // or whatever format you prefer
}
});
您可以创建一个注册助手来处理日期格式:
helper.js
Template.registerHelper('FormatDate', function(date){
return moment(date).format("MMMM Do YYYY")
})
现在您可以:
{{#each posts}}
<li>{{FormateDate date}}</li>
{{/each}}
所以我在模板中有这样的东西:
{{#each posts}}
<li>{{date}}</li>
{{/each}}
这显示正常,但问题是我的 "date" 变量显示为 Sat Feb 07 2015 19:47:13 GMT-0800 (PST)
,这是准确的,但有点长且不必要。
我希望日期看起来更简单,例如 February 7, 2015
。但是,请原谅我的不理解,但我知道如何做到这一点的唯一方法是开始时以漂亮的格式存储它,但如果我必须这样做,这似乎效率不高每次我想让东西看起来有点不同。
理想情况下,我想马上将它包装起来 () 并生成如下内容:
moment(date).format("MMMM Do YYYY")
我试着改用这个,但它不起作用:
{{#each posts}}
<li>{{moment(date).format("MMMM Do YYYY")}}</li>
{{/each}}
试试这个。
Template.example.rendered = function(){
//here we are getting a new date
var d = moment(new Date());
//here we take the li element and we set it on the html
document.getElementById("dateFormated").innerHTML = d.format('LL');
}
<template name="example">
<li id="dateFormated"> </li>
</template>
更新
因为您使用的是 {{#each}} 迭代器
你可以做到。
var d = moment(new Date()).format('LL');
或
var d = moment(new Date());
dFormat = d.format('LL');
在插入时,插入一个像Posts.insert({submitted:dFormat})
并且在 {{each}}
通话中,就像您
{{#each posts}}
<li>{{subbmited}}</li>
{{/each}}
不插入数据库(另一种选择)
Template.example.helpers({
formattedData:function(){
return moment(new Date()).format('LL');
}
})
{{#each posts}}
<li>{{formattedData}}</li>
{{/each}}
添加助手
Template.yourTemplate.helpers({
formattedDate: function(){
return moment(this.date).format("MM/DD/YYYY"); // or whatever format you prefer
}
});
您可以创建一个注册助手来处理日期格式:
helper.js
Template.registerHelper('FormatDate', function(date){
return moment(date).format("MMMM Do YYYY")
})
现在您可以:
{{#each posts}}
<li>{{FormateDate date}}</li>
{{/each}}