mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
merging a variant of svieira's fix for preserving whitespace within templates.
This commit is contained in:
@@ -69,6 +69,9 @@ $(document).ready(function() {
|
|||||||
var quoteInStatementAndBody = _.template("<% if(foo == 'bar'){ %>Statement quotes and 'quotes'.<% } %>");
|
var quoteInStatementAndBody = _.template("<% if(foo == 'bar'){ %>Statement quotes and 'quotes'.<% } %>");
|
||||||
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
||||||
|
|
||||||
|
var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');
|
||||||
|
equals(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
|
||||||
|
|
||||||
_.templateSettings = {
|
_.templateSettings = {
|
||||||
start : '{{',
|
start : '{{',
|
||||||
end : '}}',
|
end : '}}',
|
||||||
|
|||||||
@@ -625,17 +625,19 @@
|
|||||||
// JavaScript templating a-la ERB, pilfered from John Resig's
|
// JavaScript templating a-la ERB, pilfered from John Resig's
|
||||||
// "Secrets of the JavaScript Ninja", page 83.
|
// "Secrets of the JavaScript Ninja", page 83.
|
||||||
// Single-quote fix from Rick Strahl's version.
|
// Single-quote fix from Rick Strahl's version.
|
||||||
// With alterations for arbitrary delimiters.
|
// With alterations for arbitrary delimiters, and to preserve whitespace.
|
||||||
_.template = function(str, data) {
|
_.template = function(str, data) {
|
||||||
var c = _.templateSettings;
|
var c = _.templateSettings;
|
||||||
var endMatch = new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+escapeRegExp(c.end)+")","g");
|
var endMatch = new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+escapeRegExp(c.end)+")","g");
|
||||||
var fn = new Function('obj',
|
var fn = new Function('obj',
|
||||||
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
||||||
'with(obj||{}){p.push(\'' +
|
'with(obj||{}){p.push(\'' +
|
||||||
str.replace(/[\r\t\n]/g, " ")
|
str.replace(/\r/g, '\\r')
|
||||||
.replace(endMatch,"\t")
|
.replace(/\n/g, '\\n')
|
||||||
|
.replace(/\t/g, '\\t')
|
||||||
|
.replace(endMatch,"✄")
|
||||||
.split("'").join("\\'")
|
.split("'").join("\\'")
|
||||||
.split("\t").join("'")
|
.split("✄").join("'")
|
||||||
.replace(c.interpolate, "',$1,'")
|
.replace(c.interpolate, "',$1,'")
|
||||||
.split(c.start).join("');")
|
.split(c.start).join("');")
|
||||||
.split(c.end).join("p.push('")
|
.split(c.end).join("p.push('")
|
||||||
|
|||||||
Reference in New Issue
Block a user