template.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. var bareIdentifier = /^\s*(\w|\$)+\s*$/;
  21. // JavaScript micro-templating, similar to John Resig's implementation.
  22. // Underscore templating handles arbitrary delimiters, preserves whitespace,
  23. // and correctly escapes quotes within interpolated code.
  24. // NB: `oldSettings` only exists for backwards compatibility.
  25. function template(text, settings, oldSettings) {
  26. if (!settings && oldSettings) settings = oldSettings;
  27. settings = defaults({}, settings, underscore.templateSettings);
  28. // Combine delimiters into one regular expression via alternation.
  29. var matcher = RegExp([
  30. (settings.escape || noMatch).source,
  31. (settings.interpolate || noMatch).source,
  32. (settings.evaluate || noMatch).source
  33. ].join('|') + '|$', 'g');
  34. // Compile the template source, escaping string literals appropriately.
  35. var index = 0;
  36. var source = "__p+='";
  37. text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
  38. source += text.slice(index, offset).replace(escapeRegExp, escapeChar);
  39. index = offset + match.length;
  40. if (escape) {
  41. source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
  42. } else if (interpolate) {
  43. source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
  44. } else if (evaluate) {
  45. source += "';\n" + evaluate + "\n__p+='";
  46. }
  47. // Adobe VMs need the match returned to produce the correct offset.
  48. return match;
  49. });
  50. source += "';\n";
  51. var argument = settings.variable;
  52. if (argument) {
  53. if (!bareIdentifier.test(argument)) throw new Error(argument);
  54. } else {
  55. // If a variable is not specified, place data values in local scope.
  56. source = 'with(obj||{}){\n' + source + '}\n';
  57. argument = 'obj';
  58. }
  59. source = "var __t,__p='',__j=Array.prototype.join," +
  60. "print=function(){__p+=__j.call(arguments,'');};\n" +
  61. source + 'return __p;\n';
  62. var render;
  63. try {
  64. render = new Function(argument, '_', source);
  65. } catch (e) {
  66. e.source = source;
  67. throw e;
  68. }
  69. var template = function(data) {
  70. return render.call(this, data, underscore);
  71. };
  72. // Provide the compiled source as a convenience for precompilation.
  73. template.source = 'function(' + argument + '){\n' + source + '}';
  74. return template;
  75. }
  76. return template;
  77. });