Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

template.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. define(['./underscore', './defaults', './templateSettings'], function (underscore, defaults, templateSettings) {
  2. // When customizing `_.templateSettings`, if you don't want to define an
  3. // interpolation, evaluation or escaping regex, we need one that is
  4. // guaranteed not to match.
  5. var noMatch = /(.)^/;
  6. // Certain characters need to be escaped so that they can be put into a
  7. // string literal.
  8. var escapes = {
  9. "'": "'",
  10. '\\': '\\',
  11. '\r': 'r',
  12. '\n': 'n',
  13. '\u2028': 'u2028',
  14. '\u2029': 'u2029'
  15. };
  16. var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g;
  17. function escapeChar(match) {
  18. return '\\' + escapes[match];
  19. }
  20. // JavaScript micro-templating, similar to John Resig's implementation.
  21. // Underscore templating handles arbitrary delimiters, preserves whitespace,
  22. // and correctly escapes quotes within interpolated code.
  23. // NB: `oldSettings` only exists for backwards compatibility.
  24. function template(text, settings, oldSettings) {
  25. if (!settings && oldSettings) settings = oldSettings;
  26. settings = defaults({}, settings, underscore.templateSettings);
  27. // Combine delimiters into one regular expression via alternation.
  28. var matcher = RegExp([
  29. (settings.escape || noMatch).source,
  30. (settings.interpolate || noMatch).source,
  31. (settings.evaluate || noMatch).source
  32. ].join('|') + '|$', 'g');
  33. // Compile the template source, escaping string literals appropriately.
  34. var index = 0;
  35. var source = "__p+='";
  36. text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
  37. source += text.slice(index, offset).replace(escapeRegExp, escapeChar);
  38. index = offset + match.length;
  39. if (escape) {
  40. source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
  41. } else if (interpolate) {
  42. source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
  43. } else if (evaluate) {
  44. source += "';\n" + evaluate + "\n__p+='";
  45. }
  46. // Adobe VMs need the match returned to produce the correct offset.
  47. return match;
  48. });
  49. source += "';\n";
  50. // If a variable is not specified, place data values in local scope.
  51. if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
  52. source = "var __t,__p='',__j=Array.prototype.join," +
  53. "print=function(){__p+=__j.call(arguments,'');};\n" +
  54. source + 'return __p;\n';
  55. var render;
  56. try {
  57. render = new Function(settings.variable || 'obj', '_', source);
  58. } catch (e) {
  59. e.source = source;
  60. throw e;
  61. }
  62. var template = function(data) {
  63. return render.call(this, data, underscore);
  64. };
  65. // Provide the compiled source as a convenience for precompilation.
  66. var argument = settings.variable || 'obj';
  67. template.source = 'function(' + argument + '){\n' + source + '}';
  68. return template;
  69. }
  70. return template;
  71. });