You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

70 lines
1.2 KiB

  1. var toString = function (value, type) {
  2. if (typeof value !== 'string') {
  3. type = typeof value;
  4. if (type === 'number') {
  5. value += '';
  6. } else if (type === 'function') {
  7. value = toString(value.call(value));
  8. } else {
  9. value = '';
  10. }
  11. }
  12. return value;
  13. };
  14. var escapeMap = {
  15. "<": "&#60;",
  16. ">": "&#62;",
  17. '"': "&#34;",
  18. "'": "&#39;",
  19. "&": "&#38;"
  20. };
  21. var escapeFn = function (s) {
  22. return escapeMap[s];
  23. };
  24. var escapeHTML = function (content) {
  25. return toString(content)
  26. .replace(/&(?![\w#]+;)|[<>"']/g, escapeFn);
  27. };
  28. var isArray = Array.isArray || function (obj) {
  29. return ({}).toString.call(obj) === '[object Array]';
  30. };
  31. var each = function (data, callback) {
  32. var i, len;
  33. if (isArray(data)) {
  34. for (i = 0, len = data.length; i < len; i++) {
  35. callback.call(data, data[i], i, data);
  36. }
  37. } else {
  38. for (i in data) {
  39. callback.call(data, data[i], i);
  40. }
  41. }
  42. };
  43. var utils = template.utils = {
  44. $helpers: {},
  45. $include: renderFile,
  46. $string: toString,
  47. $escape: escapeHTML,
  48. $each: each
  49. };