/branches/version1.x/app/JsPlate.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 135 lines · 111 code · 7 blank · 17 comment · 1 complexity · 760d780bf5924543a1b5a637c1baa834 MD5 · raw file

  1. /**
  2. * @fileOverview
  3. * @name JsPlate
  4. * @author Michael Mathews micmath@gmail.com
  5. * @url $HeadURL: http://jsdoc-toolkit.googlecode.com/svn/branches/version1.x/app/JsPlate.js $
  6. * @revision $Id: JsPlate.js 389 2007-12-26 17:26:27Z micmath $
  7. * @license <a href="http://en.wikipedia.org/wiki/MIT_License">X11/MIT License</a>
  8. * (See the accompanying README file for full details.)
  9. */
  10. /**
  11. * @class A lightweight template engine for JavaScript.
  12. * @constructor
  13. * @author Michael Mathews <a href="mailto:micmath@gmail.com">micmath@gmail.com</a>
  14. * @param {string} template
  15. */
  16. JsPlate = function(templateFile) {
  17. if (templateFile) this.template = IO.readFile(templateFile);
  18. this.templateFile = templateFile;
  19. this.code = "";
  20. this.parse();
  21. }
  22. /** Converts a template into evalable code. */
  23. JsPlate.prototype.parse = function() {
  24. this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
  25. this.code = "var output=``"+this.template;
  26. this.code = this.code.replace(
  27. /<for +each="(.+?)" +in="(.+?)" *>/gi,
  28. function (match, eachName, inName) {
  29. return "``;\rvar $"+eachName+"_keys = keys("+inName+");\rfor(var $"+eachName+"_i = 0; $"+eachName+"_i < $"+eachName+"_keys.length; $"+eachName+"_i++) {\rvar $"+eachName+"_last = ($"+eachName+"_i == $"+eachName+"_keys.length-1);\rvar $"+eachName+"_key = $"+eachName+"_keys[$"+eachName+"_i];\rvar "+eachName+" = "+inName+"[$"+eachName+"_key];\routput+=``";
  30. }
  31. );
  32. this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { output+=``");
  33. this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
  34. this.code = this.code.replace(
  35. /\{\+\s*([\s\S]+?)\s*\+\}/gi,
  36. function (match, code) {
  37. code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
  38. code = code.replace(/(\r?\n)/g, " ");
  39. return "``+"+code+"+``";
  40. }
  41. );
  42. this.code = this.code.replace(
  43. /\{!\s*([\s\S]+?)\s*!\}/gi,
  44. function (match, code) {
  45. code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
  46. code = code.replace(/(\n)/g, " ");
  47. return "``; "+code+";\routput+=``";
  48. }
  49. );
  50. this.code = this.code+"``;";
  51. this.code = this.code.replace(/(\r?\n)/g, "\\n");
  52. this.code = this.code.replace(/"/g, "\\\"");
  53. this.code = this.code.replace(/``/g, "\"");
  54. }
  55. /**
  56. * @private
  57. */
  58. JsPlate.prototype.toCode = function() {
  59. return this.code;
  60. }
  61. /**
  62. * @private
  63. * @static
  64. * @memberOf JsPlate
  65. */
  66. JsPlate.keys = function(obj) {
  67. var keys = [];
  68. // TODO: Confirm that arrays are not treated as objects
  69. if (obj.constructor.toString().indexOf("Array") > -1) {
  70. for (var i = 0; i < obj.length; i++) {
  71. keys.push(i);
  72. }
  73. }
  74. else {
  75. for (var i in obj) {
  76. keys.push(i);
  77. }
  78. }
  79. return keys;
  80. };
  81. /**
  82. * @private
  83. * @static
  84. * @memberOf JsPlate
  85. */
  86. JsPlate.values = function(obj) {
  87. var values = [];
  88. // TODO: Confirm that arrays are not treated as objects
  89. if (obj.constructor.toString().indexOf("Array") > -1) {
  90. for (var i = 0; i < obj.length; i++) {
  91. values.push(obj[i]);
  92. }
  93. }
  94. else {
  95. for (var i in obj) {
  96. values.push(obj[i]);
  97. }
  98. }
  99. return values;
  100. };
  101. /**
  102. * Return the output. This must be called after parse()
  103. * @param {object} data What shall represent the "data" in your template.
  104. * @return {string}
  105. */
  106. JsPlate.prototype.process = function(data) {
  107. var keys = JsPlate.keys;
  108. var values = JsPlate.values;
  109. try {
  110. eval(this.code);
  111. }
  112. catch (e) {
  113. print(">> There was an error evaluating the compiled code from template: "+this.templateFile);
  114. print(" The error was on line "+e.lineNumber+" "+e.name+": "+e.message);
  115. var lines = this.code.split("\r");
  116. if (e.lineNumber-2 >= 0) print("line "+(e.lineNumber-1)+": "+lines[e.lineNumber-2]);
  117. print("line "+e.lineNumber+": "+lines[e.lineNumber-1]);
  118. print("");
  119. }
  120. return output;
  121. //print(this.code);
  122. }