MCPcopy Create free account
hub / github.com/ddopson/underscore-cli / parseTemplate

Function parseTemplate

lib/template.js:59–90  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

57
58// DDOPSON-2012-04-29 - The method below is written such that the line numbers are in perfect 1:1 correlation between the .template file and the generated JS code. This means that stacks will have correct line numbers
59function parseTemplate(text) {
60 var settings = _.templateSettings;
61
62 // Compile the template source, taking care to escape characters that
63 // cannot be included in a string literal and then unescape them in code
64 // blocks.
65
66 var source = "__p+='" + text
67 .replace(escaper, function(match) {
68 return '\\' + escapes[match];
69 })
70 .replace(settings.escape || noMatch, function(match, code) {
71 return "'+_.escape(" + unescape(code) + ")+'";
72 })
73 .replace(settings.interpolate || noMatch, function(match, code) {
74 return "'+(" + unescape(code) + ")+'";
75 })
76 .replace(settings.evaluate || noMatch, function(match, code) {
77 return "';" + unescape(code) + ";__p+='";
78 }) + "';";
79
80 // If a variable is not specified, place data values in local scope.
81 if (!settings.variable) {
82 source = 'with(obj||{}){' + source + '}';
83 }
84
85 source = 'function(' + (settings.variable || 'obj') + '){' +
86 "var __p='', print=function(){__p+=Array.prototype.join.call(arguments, '')};" +
87 source + " return __p; }";
88 source = source.replace(/\\N/g, "\\n'+\n'");
89 return source;
90}
91
92
93// when loaded as require('foo.template'), text == undefined

Callers 1

template.jsFile · 0.85

Calls 1

unescapeFunction · 0.85

Tested by

no test coverage detected