mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
Fixing Issue #35. newlines in evaluated code.
This commit is contained in:
@@ -1065,17 +1065,15 @@ _.template(list, {people : ['moe', 'curly', 'larry']});
|
||||
<p>
|
||||
If ERB-style delimiters aren't your cup of tea, you can change Underscore's
|
||||
template settings to use different symbols to set off interpolated code.
|
||||
Define <b>start</b> and <b>end</b> tokens, and an <b>interpolate</b> regex
|
||||
to match expressions that should be evaluated and inserted. For example,
|
||||
to perform
|
||||
Define and an <b>interpolate</b> regex, and an (optional) <b>evaluate</b> regex
|
||||
to match expressions that should be inserted and evaluated, respectively.
|
||||
For example, to perform
|
||||
<a href="http://github.com/janl/mustache.js#readme">Mustache.js</a>
|
||||
style templating:
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
_.templateSettings = {
|
||||
start : '{{',
|
||||
end : '}}',
|
||||
interpolate : /\{\{(.+?)\}\}/g
|
||||
};
|
||||
|
||||
|
||||
@@ -55,7 +55,9 @@ $(document).ready(function() {
|
||||
var result = basicTemplate({thing : 'This'});
|
||||
equals(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');
|
||||
|
||||
var fancyTemplate = _.template("<ul><% for (key in people) { %><li><%= people[key] %></li><% } %></ul>");
|
||||
var fancyTemplate = _.template("<ul><% \
|
||||
for (key in people) { \
|
||||
%><li><%= people[key] %></li><% } %></ul>");
|
||||
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
|
||||
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
||||
|
||||
@@ -84,8 +86,7 @@ $(document).ready(function() {
|
||||
equals(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
|
||||
|
||||
_.templateSettings = {
|
||||
start : '{{',
|
||||
end : '}}',
|
||||
evaluate : /\{\{(.+?)\}\}/g,
|
||||
interpolate : /\{\{=(.+?)\}\}/g
|
||||
};
|
||||
|
||||
@@ -100,8 +101,7 @@ $(document).ready(function() {
|
||||
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||
|
||||
_.templateSettings = {
|
||||
start : '<?',
|
||||
end : '?>',
|
||||
evaluate : /<\?(.+?)\?>/g,
|
||||
interpolate : /<\?=(.+?)\?>/g
|
||||
};
|
||||
|
||||
@@ -116,8 +116,6 @@ $(document).ready(function() {
|
||||
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||
|
||||
_.templateSettings = {
|
||||
start : '{{',
|
||||
end : '}}',
|
||||
interpolate : /\{\{(.+?)\}\}/g
|
||||
};
|
||||
|
||||
|
||||
@@ -617,8 +617,7 @@
|
||||
// By default, Underscore uses ERB-style template delimiters, change the
|
||||
// following template settings to use alternative delimiters.
|
||||
_.templateSettings = {
|
||||
start : '<%',
|
||||
end : '%>',
|
||||
evaluate : /<%(.+?)%>/g,
|
||||
interpolate : /<%=(.+?)%>/g
|
||||
};
|
||||
|
||||
@@ -628,21 +627,22 @@
|
||||
// With alterations for arbitrary delimiters, and to preserve whitespace.
|
||||
_.template = function(str, data) {
|
||||
var c = _.templateSettings;
|
||||
var endMatch = new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+escapeRegExp(c.end)+")","g");
|
||||
var fn = new Function('obj',
|
||||
'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
|
||||
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
|
||||
'with(obj||{}){__p.push(\'' +
|
||||
str.replace(/\r/g, '\\r')
|
||||
str.split("'").join("\\'")
|
||||
.replace(c.interpolate, function(match, code) {
|
||||
return "'," + code.replace(/\\'/g, "'") + ",'";
|
||||
})
|
||||
.replace(c.evaluate || null, function(match, code) {
|
||||
return "');" + code.replace(/\\'/g, "'")
|
||||
.replace(/[\r\n\t]/g, ' ') + "__p.push('";
|
||||
})
|
||||
.replace(/\r/g, '\\r')
|
||||
.replace(/\n/g, '\\n')
|
||||
.replace(/\t/g, '\\t')
|
||||
.replace(endMatch,"✄")
|
||||
.split("'").join("\\'")
|
||||
.split("✄").join("'")
|
||||
.replace(c.interpolate, "',$1,'")
|
||||
.split(c.start).join("');")
|
||||
.split(c.end).join("__p.push('")
|
||||
+ "');}return __p.join('');");
|
||||
return data ? fn(data) : fn;
|
||||
+ "');}return __p.join('');";
|
||||
var func = new Function('obj', tmpl);
|
||||
return data ? func(data) : func;
|
||||
};
|
||||
|
||||
// ------------------------------- Aliases ----------------------------------
|
||||
|
||||
Reference in New Issue
Block a user