art-template过滤器处理时间戳
1. 引入art-template.js
2.
<script src="/static/art-template.js"></script>
2.写模板
<script type="text/html" id="artlist">
{{each data.list val}}
<span>{{val.createdate | dateFormat 'yyyy-MM-dd hh:mm:ss'}}</span>
{{/each}}
</script>
3.在入口函数中加入过滤器函数
template.defaults.imports.dateFormat = function(date, format) {
date = new Date(date);
var map = {
"M": date.getMonth() + 1,
"d": date.getDate(),
"h": date.getHours(),
"m": date.getMinutes(),
"s": date.getSeconds(),
"q": Math.floor((date.getMonth() + 3) / 3),
"S": date.getMilliseconds()
};
format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
var v = map[t];
if (v !== undefined) {
if (all.length > 1) {
v = '0' + v;
v = v.substr(v.length - 2);
}
return v;
} else if (t === 'y') {
return (date.getFullYear() + '').substr(4 - all.length);
}
return all;
});
return format;
};
4.请求数据,赋值给模板
getTemplate();
function getTemplate() {
$.ajax({
url: '****',
type: 'get',
success: function (val) {
if (val.code == 0) {
var temp = template('artlist', val)
$('#theme').html(temp)
}
}
})
}