Skip to content

Commit 5de7ec1

Browse files
committed
Merge pull request #117 from az7arul/template-replace-non-existant-key
.template method will replace keys that do not exist with empty string
2 parents a78f8b3 + b179720 commit 5de7ec1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/string.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
418418

419419
matches.forEach(function(match) {
420420
var key = match.substring(opening.length, match.length - closing.length).trim();//chop {{ and }}
421-
if (typeof values[key] != 'undefined')
422-
s = s.replace(match, values[key]);
421+
var value = typeof values[key] == 'undefined' ? '' : values[key];
422+
s = s.replace(match, value);
423423
});
424424
return new this.constructor(s);
425425
},

test/string.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,16 @@
547547
EQ (S(str).template(values).s, "Hello ")
548548
})
549549
})
550+
551+
describe('> when a key does not exist', function() {
552+
it('should still replace with the empty value', function() {
553+
S.TMPL_OPEN = '{{'
554+
S.TMPL_CLOSE = '}}'
555+
var str = "Hello {{name}}"
556+
var values = {}
557+
EQ (S(str).template(values).s, "Hello ")
558+
})
559+
})
550560
})
551561

552562
describe('- times(n)', function() {

0 commit comments

Comments
 (0)