Skip to content

Commit 61c0181

Browse files
committed
Merge pull request #119 from az7arul/template-improvements
remove leading and trailing spaces before interpolating key
2 parents 3252a03 + e8c88eb commit 61c0181

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

lib/string.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ string.js - Copyright (C) 2012-2014, JP Richardson <[email protected]>
417417
var matches = s.match(r) || [];
418418

419419
matches.forEach(function(match) {
420-
var key = match.substring(opening.length, match.length - closing.length);//chop {{ and }}
420+
var key = match.substring(opening.length, match.length - closing.length).trim();//chop {{ and }}
421421
if (typeof values[key] != 'undefined')
422422
s = s.replace(match, values[key]);
423423
});

test/string.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,12 @@
491491
describe('- template(values, [open], [close])', function() {
492492
it('should return the string replaced with template values', function() {
493493
var str = "Hello {{name}}! How are you doing during the year of {{date-year}}?"
494-
var values = {name: 'JP', 'date-year': 2013}
494+
var values = {greet: 'Hello', name: 'JP', 'date-year': 2013}
495495
EQ (S(str).template(values).s, 'Hello JP! How are you doing during the year of 2013?')
496496

497+
var str = "{{greet }} {{ name}}! How are you doing during the year of {{ date-year }}?";
498+
EQ (S(str).template(values).s, 'Hello JP! How are you doing during the year of 2013?')
499+
497500
str = "Hello #{name}! How are you doing during the year of #{date-year}?"
498501
EQ (S(str).template(values, '#{', '}').s, 'Hello JP! How are you doing during the year of 2013?')
499502

0 commit comments

Comments
 (0)