template.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Template - Very Very Simple Template Engine
  3. * similar prototype.js template engine
  4. * add syntax > #{for:name:maxCount:cutCount} template #{/for:name}
  5. * add syntax > #{if:name sign value} template #{/if:name}
  6. */
  7. (function(){
  8. function evaluate(data, tpl) {
  9. if (!data) {
  10. return '';
  11. }
  12. if (tpl.indexOf("\{if:") > -1) {
  13. tpl = tpl.replace(/#\{if:([_\w]+)([=><!]+)([_'"\-\w]+)\}([\s\S]*?)#\{\/if:\1\}/gm, function(full, start, sign, value, condtpl){
  14. if (data[start] == _NULL) {
  15. return full;
  16. }
  17. var _condition = _FALSE;
  18. try {
  19. sign = ((sign=="=")? "==": sign);
  20. var _left = "\"" + (data[start] + "").replace(/['"]/g, "") + "\"";
  21. var _right = "\"" + value.replace(/['"]/g, "") + "\"";
  22. _condition = txEval("(" + _left + sign + _right + ")");
  23. }catch(e) { _condition = _FALSE; }
  24. if(_condition) {
  25. return evaluate(data, condtpl);
  26. } else {
  27. return "";
  28. }
  29. });
  30. }
  31. if (tpl.indexOf("\{for:") > -1) {
  32. tpl = tpl.replace(/#\{for:([_\w]+):?(\d*):?(\d*)\}([\s\S]*?)#\{\/for:\1\}/gm, function(full, start, maxCnt, cutCnt, looptpl) {
  33. if (!data[start] || !data[start].length) {
  34. return full;
  35. }
  36. var _list = data[start];
  37. var _listTpl = [];
  38. maxCnt = !!maxCnt? (isNaN(maxCnt)? _list.length: parseInt(maxCnt)): _list.length;
  39. cutCnt = !!cutCnt? (isNaN(cutCnt)? 0: parseInt(cutCnt)): 0;
  40. for (var i = 0, len = Math.min(_list.length, maxCnt); i < len; i++) {
  41. _listTpl.push(evaluate(_list[i], looptpl));
  42. }
  43. return _listTpl.join("").substring(cutCnt);
  44. });
  45. }
  46. return tpl.replace(/#\{([_\w]+)\}/g, function(full, name) {
  47. if(data[name] != _NULL) {
  48. return data[name];
  49. } else {
  50. return full;
  51. }
  52. });
  53. }
  54. var tmp = _WIN.Template = function(template) {
  55. this.template = template;
  56. };
  57. tmp.prototype = {
  58. evaluate : function(data) {
  59. return evaluate(data, this.template);
  60. },
  61. evaluateToDom : function(data, element) {
  62. if(typeof(element) === 'string') {
  63. element = _DOC.getElementById(element);
  64. }
  65. element.innerHTML = evaluate(data, this.template);
  66. },
  67. evaluateAsDom : function(data, context) {
  68. var _tmpNode = (context || document).createElement('div');
  69. _tmpNode.innerHTML = evaluate(data, this.template);
  70. return _tmpNode.firstChild;
  71. }
  72. };
  73. /*
  74. Template.prototype.evaluate = function(data) {
  75. return evaluate(data, this.template);
  76. };
  77. Template.prototype.evaluateToDom = function(data, element) {
  78. if(typeof(element) === 'string') {
  79. element = _DOC.getElementById(element);
  80. }
  81. element.innerHTML = evaluate(data, this.template);
  82. };
  83. Template.prototype.evaluateAsDom = function(data, context) {
  84. var _tmpNode = (context || document).createElement('div');
  85. _tmpNode.innerHTML = evaluate(data, this.template);
  86. return _tmpNode.firstChild;
  87. };
  88. */
  89. })();