@@ -11,8 +11,9 @@ var isArray = require('../type/isArray');
1111var isString = require ( '../type/isString' ) ;
1212var extend = require ( '../object/extend' ) ;
1313
14- var EXPRESSION_REGEXP = / { { \s ? ( \/ ? [ a - z A - Z 0 - 9 _ . @ [ \] ] + ) \s ? } } / g;
15- var BRACKET_REGEXP = / ^ ( [ a - z A - Z 0 - 9 _ @ ] + ) \[ ( [ a - z A - Z 0 - 9 _ @ ] + ) \] $ / ;
14+ var EXPRESSION_REGEXP = / { { \s ? ( \/ ? [ a - z A - Z 0 - 9 _ . @ [ \] " ' ] + ) \s ? } } / g;
15+ var BRACKET_REGEXP = / ^ ( [ a - z A - Z 0 - 9 _ @ ] + ) \[ ( [ a - z A - Z 0 - 9 _ @ " ' ] + ) \] $ / ;
16+ var STRING_REGEXP = / ^ [ " ' ] ( \w + ) [ " ' ] $ / ;
1617var NUMBER_REGEXP = / ^ - ? \d + \. ? \d * $ / ;
1718
1819var 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'.
0 commit comments