如何在dust.js模板中格式化数字、货币或日期值?
数据:
{
today: 'Wed Apr 03 2013 10:23:34 GMT+0200 (CEST)'
}模板:
<p>Today: {today} </p>像这样的:(与moment.js)
<p>Today: {moment(today).format('dd.MM.YYYY')}</p>或四舍五入的价格值*
数据:{价格: 56.23423425 }
模板:
价格:{price.toFixed(2)}
发布于 2013-04-05 22:06:55
您可能需要编写一个助手。有关如何编写助手的详细信息,请参见以下内容:
日期字符串的模板如下所示:
<p>Today: {@formatDate value="{today}"/}</p>你的帮手应该是这样的:
dust.helpers.formatDate = function (chunk, context, bodies, params) {
var value = dust.helpers.tap(params.value, chunk, context),
timestamp,
month,
date,
year;
timestamp = new Date(value);
month = timestamp.getMonth() + 1;
date = timestamp.getDate();
year = timestamp.getFullYear();
return chunk.write(date + '.' + month + '.' + year);
};您可能需要添加在前面的一个月或日期前的零位。
发布于 2014-01-06 07:20:56
发布于 2017-02-23 02:50:06
你可以写一个过滤器来使用瞬间。写成这样: dust.filters.formatDate = (value) => moment.utc(value).format('l :mm‘);在脚本中合适的地方。然后,在html中,只需在值旁边放一个\\:{date\formatDate}
https://stackoverflow.com/questions/15782312
复制相似问题