tie*_*enb 6 javascript client-side-templating dust.js
如何在dust.js模板中格式化数字,货币或日期值?
数据:
{
today: 'Wed Apr 03 2013 10:23:34 GMT+0200 (CEST)'
}
Run Code Online (Sandbox Code Playgroud)
模板:
<p>Today: {today} </p>
Run Code Online (Sandbox Code Playgroud)
像这样:(与moment.js)
<p>Today: {moment(today).format('dd.MM.YYYY')}</p>
Run Code Online (Sandbox Code Playgroud)
或者围绕一些价格值*
数据:{价格:56.23423425}
模板:
价格:{price.toFixed(2)}
您可能需要编写帮助程序.有关如何编写帮助程序的详细信息,请访问:
Date字符串的模板如下所示:
<p>Today: {@formatDate value="{today}"/}</p>
Run Code Online (Sandbox Code Playgroud)
你的助手将是这样的:
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);
};
Run Code Online (Sandbox Code Playgroud)
您可能希望添加该片段以获得月前或日期前的前导零.