Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

224 rader
5.8 KiB

  1. // lib/handlebars/base.js
  2. var Handlebars = {};
  3. Handlebars.VERSION = "1.0.beta.6";
  4. Handlebars.helpers = {};
  5. Handlebars.partials = {};
  6. Handlebars.registerHelper = function(name, fn, inverse) {
  7. if(inverse) { fn.not = inverse; }
  8. this.helpers[name] = fn;
  9. };
  10. Handlebars.registerPartial = function(name, str) {
  11. this.partials[name] = str;
  12. };
  13. Handlebars.registerHelper('helperMissing', function(arg) {
  14. if(arguments.length === 2) {
  15. return undefined;
  16. } else {
  17. throw new Error("Could not find property '" + arg + "'");
  18. }
  19. });
  20. var toString = Object.prototype.toString, functionType = "[object Function]";
  21. Handlebars.registerHelper('blockHelperMissing', function(context, options) {
  22. var inverse = options.inverse || function() {}, fn = options.fn;
  23. var ret = "";
  24. var type = toString.call(context);
  25. if(type === functionType) { context = context.call(this); }
  26. if(context === true) {
  27. return fn(this);
  28. } else if(context === false || context == null) {
  29. return inverse(this);
  30. } else if(type === "[object Array]") {
  31. if(context.length > 0) {
  32. for(var i=0, j=context.length; i<j; i++) {
  33. ret = ret + fn(context[i]);
  34. }
  35. } else {
  36. ret = inverse(this);
  37. }
  38. return ret;
  39. } else {
  40. return fn(context);
  41. }
  42. });
  43. Handlebars.registerHelper('each', function(context, options) {
  44. var fn = options.fn, inverse = options.inverse;
  45. var ret = "";
  46. if(context && context.length > 0) {
  47. for(var i=0, j=context.length; i<j; i++) {
  48. ret = ret + fn(context[i]);
  49. }
  50. } else {
  51. ret = inverse(this);
  52. }
  53. return ret;
  54. });
  55. Handlebars.registerHelper('if', function(context, options) {
  56. var type = toString.call(context);
  57. if(type === functionType) { context = context.call(this); }
  58. if(!context || Handlebars.Utils.isEmpty(context)) {
  59. return options.inverse(this);
  60. } else {
  61. return options.fn(this);
  62. }
  63. });
  64. Handlebars.registerHelper('unless', function(context, options) {
  65. var fn = options.fn, inverse = options.inverse;
  66. options.fn = inverse;
  67. options.inverse = fn;
  68. return Handlebars.helpers['if'].call(this, context, options);
  69. });
  70. Handlebars.registerHelper('with', function(context, options) {
  71. return options.fn(context);
  72. });
  73. Handlebars.registerHelper('log', function(context) {
  74. Handlebars.log(context);
  75. });
  76. ;
  77. // lib/handlebars/utils.js
  78. Handlebars.Exception = function(message) {
  79. var tmp = Error.prototype.constructor.apply(this, arguments);
  80. for (var p in tmp) {
  81. if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
  82. }
  83. this.message = tmp.message;
  84. };
  85. Handlebars.Exception.prototype = new Error;
  86. // Build out our basic SafeString type
  87. Handlebars.SafeString = function(string) {
  88. this.string = string;
  89. };
  90. Handlebars.SafeString.prototype.toString = function() {
  91. return this.string.toString();
  92. };
  93. (function() {
  94. var escape = {
  95. "<": "&lt;",
  96. ">": "&gt;",
  97. '"': "&quot;",
  98. "'": "&#x27;",
  99. "`": "&#x60;"
  100. };
  101. var badChars = /&(?!\w+;)|[<>"'`]/g;
  102. var possible = /[&<>"'`]/;
  103. var escapeChar = function(chr) {
  104. return escape[chr] || "&amp;";
  105. };
  106. Handlebars.Utils = {
  107. escapeExpression: function(string) {
  108. // don't escape SafeStrings, since they're already safe
  109. if (string instanceof Handlebars.SafeString) {
  110. return string.toString();
  111. } else if (string == null || string === false) {
  112. return "";
  113. }
  114. if(!possible.test(string)) { return string; }
  115. return string.replace(badChars, escapeChar);
  116. },
  117. isEmpty: function(value) {
  118. if (typeof value === "undefined") {
  119. return true;
  120. } else if (value === null) {
  121. return true;
  122. } else if (value === false) {
  123. return true;
  124. } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. };
  131. })();;
  132. // lib/handlebars/runtime.js
  133. Handlebars.VM = {
  134. template: function(templateSpec) {
  135. // Just add water
  136. var container = {
  137. escapeExpression: Handlebars.Utils.escapeExpression,
  138. invokePartial: Handlebars.VM.invokePartial,
  139. programs: [],
  140. program: function(i, fn, data) {
  141. var programWrapper = this.programs[i];
  142. if(data) {
  143. return Handlebars.VM.program(fn, data);
  144. } else if(programWrapper) {
  145. return programWrapper;
  146. } else {
  147. programWrapper = this.programs[i] = Handlebars.VM.program(fn);
  148. return programWrapper;
  149. }
  150. },
  151. programWithDepth: Handlebars.VM.programWithDepth,
  152. noop: Handlebars.VM.noop
  153. };
  154. return function(context, options) {
  155. options = options || {};
  156. return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
  157. };
  158. },
  159. programWithDepth: function(fn, data, $depth) {
  160. var args = Array.prototype.slice.call(arguments, 2);
  161. return function(context, options) {
  162. options = options || {};
  163. return fn.apply(this, [context, options.data || data].concat(args));
  164. };
  165. },
  166. program: function(fn, data) {
  167. return function(context, options) {
  168. options = options || {};
  169. return fn(context, options.data || data);
  170. };
  171. },
  172. noop: function() { return ""; },
  173. invokePartial: function(partial, name, context, helpers, partials, data) {
  174. options = { helpers: helpers, partials: partials, data: data };
  175. if(partial === undefined) {
  176. throw new Handlebars.Exception("The partial " + name + " could not be found");
  177. } else if(partial instanceof Function) {
  178. return partial(context, options);
  179. } else if (!Handlebars.compile) {
  180. throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
  181. } else {
  182. partials[name] = Handlebars.compile(partial);
  183. return partials[name](context, options);
  184. }
  185. }
  186. };
  187. Handlebars.template = Handlebars.VM.template;
  188. ;