test-helper.html 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>helper-test</title>
  6. <script src="../dist/template-debug.js"></script>
  7. </head>
  8. <body>
  9. <div id="content"></div>
  10. <script id="test" type="text/html">
  11. {{c || a || b}} {{c||a||b}}
  12. {{time | dateFormat:'yyyy年 MM月 dd日 hh:mm:ss'}}
  13. </script>
  14. <script>
  15. template.helper('dateFormat', function (date, format) {
  16. date = new Date(date);
  17. var map = {
  18. "M": date.getMonth() + 1, //月份
  19. "d": date.getDate(), //日
  20. "h": date.getHours(), //小时
  21. "m": date.getMinutes(), //分
  22. "s": date.getSeconds(), //秒
  23. "q": Math.floor((date.getMonth() + 3) / 3), //季度
  24. "S": date.getMilliseconds() //毫秒
  25. };
  26. format = format.replace(/([yMdhmsqS])+/g, function(all, t){
  27. var v = map[t];
  28. if(v !== undefined){
  29. if(all.length > 1){
  30. v = '0' + v;
  31. v = v.substr(v.length-2);
  32. }
  33. return v;
  34. }
  35. else if(t === 'y'){
  36. return (date.getFullYear() + '').substr(4 - all.length);
  37. }
  38. return all;
  39. });
  40. return format;
  41. });
  42. var data = {
  43. a: 'aaa',
  44. b: 'bbb',
  45. time: 1408535976126
  46. };
  47. var html = template('test', data);
  48. document.getElementById('content').innerHTML = html;
  49. </script>
  50. </body>
  51. </html>