Skip to content

Commit 466fa7d

Browse files
committed
feat: support bracket notation with quotes
1 parent c0c085e commit 466fa7d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

domUtil/template.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ var isArray = require('../type/isArray');
1111
var isString = require('../type/isString');
1212
var extend = require('../object/extend');
1313

14-
var EXPRESSION_REGEXP = /{{\s?(\/?[a-zA-Z0-9_.@[\] ]+)\s?}}/g;
15-
var BRACKET_REGEXP = /^([a-zA-Z0-9_@]+)\[([a-zA-Z0-9_@]+)\]$/;
14+
var EXPRESSION_REGEXP = /{{\s?(\/?[a-zA-Z0-9_.@[\]"' ]+)\s?}}/g;
15+
var BRACKET_REGEXP = /^([a-zA-Z0-9_@]+)\[([a-zA-Z0-9_@"']+)\]$/;
16+
var STRING_REGEXP = /^["'](\w+)["']$/;
1617
var NUMBER_REGEXP = /^-?\d+\.?\d*$/;
1718

1819
var EXPRESSION_INTERVAL = 2;
@@ -38,6 +39,8 @@ function getValueFromContext(exp, context) {
3839
value = true;
3940
} else if (exp === 'false') {
4041
value = false;
42+
} else if (STRING_REGEXP.test(exp)) {
43+
value = STRING_REGEXP.exec(exp)[1];
4144
} else if (BRACKET_REGEXP.test(exp)) {
4245
bracketExps = exp.split(BRACKET_REGEXP);
4346
value = getValueFromContext(bracketExps[1], context)[getValueFromContext(bracketExps[2], context)];
@@ -296,6 +299,10 @@ function compile(sources, context) {
296299
* <br>
297300
* If expression exists in the context, it will be replaced.
298301
* ex) '{{title}}' with context {title: 'Hello!'} is converted to 'Hello!'.
302+
* An array or object can be accessed using bracket notation.
303+
* ex) '{{odds[2]}}' with context {odds: [1, 3, 5]} is converted to '5'.
304+
* ex) '{{evens[first]}}' with context {evens: [2, 4], first: 0} is converted to '2'.
305+
* ex) '{{project["name"]}}' with context {project: {name: 'CodeSnippet'}} is converted to 'CodeSnippet'.
299306
* <br>
300307
* If replaced expression is a function, next expressions will be arguments of the function.
301308
* ex) '{{add 1 2}}' with context {add: function(a, b) {return a + b;}} is converted to '3'.

test/template.spec.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ describe('{{expression}}', function() {
2525

2626
it('should access the value with brackets if value is an object or array.', function() {
2727
expect(template('<p>{{ arr[2] }}</p>', {arr: [0, 1, 2]})).toBe('<p>2</p>');
28-
expect(template('<p>{{obj[key]}}</p>', {
28+
expect(template('<p>{{obj["key"]}}</p>', {obj: {key: 'value'}})).toBe('<p>value</p>');
29+
expect(template('<p>{{obj[name]}}</p>', {
2930
obj: {key: 'value'},
30-
key: 'key'
31+
name: 'key'
3132
})).toBe('<p>value</p>');
3233
expect(template('{{each nums}}{{nums[@index]}}{{/each}}', {nums: [1, 2, 3]})).toBe('123');
3334
});
@@ -36,6 +37,16 @@ describe('{{expression}}', function() {
3637
expect(template('<p>{{ false }}</p>', {})).toBe('<p>false</p>');
3738
expect(template('<p>{{true}}</p>', {})).toBe('<p>true</p>');
3839
});
40+
41+
it('should bind with string if value is string with quotes.', function() {
42+
var context = {
43+
sayHello: function(name) {
44+
return 'Hello, ' + name;
45+
}
46+
};
47+
expect(template('<p>{{ sayHello "CodeSnippet" }}</p>', context)).toBe('<p>Hello, CodeSnippet</p>');
48+
expect(template('<p>{{sayHello \'world\'}}</p>', context)).toBe('<p>Hello, world</p>');
49+
});
3950
});
4051

4152
describe('{{helper arg1 arg2}}', function() {

0 commit comments

Comments
 (0)