summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/ast.js
diff options
context:
space:
mode:
authormachty <machty@gmail.com>2013-12-24 11:01:23 -0500
committermachty <machty@gmail.com>2013-12-30 19:07:04 -0500
commitb09333db7946d20ba7dbc6d32d5496ab8295b8e1 (patch)
treeb96b31bd41c1a6c86f45e114b100c124f8217e6f /lib/handlebars/compiler/ast.js
parentac98e7b177095be80fc5d590d15b4724dd731cab (diff)
downloadhandlebars.js-b09333db7946d20ba7dbc6d32d5496ab8295b8e1.zip
handlebars.js-b09333db7946d20ba7dbc6d32d5496ab8295b8e1.tar.gz
handlebars.js-b09333db7946d20ba7dbc6d32d5496ab8295b8e1.tar.bz2
Added support for subexpressions
Handlebars now supports subexpressions. {{foo (bar 3)}} Subexpressions are always evaluated as helpers; if `3` were omitted from the above example, `bar` would be invoked as a param-less helper, even though a top-levell `{{bar}}` would be considered ambiguous. The return value of a subexpression helper is passed in as a parameter of a parent subexpression helper, even in string params mode. Their type, as listed in `options.types` or `options.hashTypes` in string params mode, is "sexpr". The main conceptual change in the Handlebars code is that there is a new AST.SexprNode that manages the params/hash passed into a mustache, as well as the logic that governs whether that mustache is a helper invocation, property lookup, etc. MustacheNode, which used to manage this stuff, still exists, but only manages things like white-space stripping and whether the mustache is escaped or not. So, a MustacheNode _has_ a SexprNode. The introduction of subexpressions is fully backwards compatible, but a few things needed to change about the compiled output of a template in order to support subexpressions. The main one is that the options hash is no longer stashed in a local `options` var before being passed to either the helper being invoked or the `helperMissing` fallback. Rather, the options object is inlined in these cases. This does mean compiled template sizes will be a little bit larger, even those that don't make use of subexpressions, but shouldn't have any noticeable impact on performance when actually rendering templates as only one of these inlined objects will actually get evaluated.
Diffstat (limited to 'lib/handlebars/compiler/ast.js')
-rw-r--r--lib/handlebars/compiler/ast.js24
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js
index b6c3a03..ce5ee11 100644
--- a/lib/handlebars/compiler/ast.js
+++ b/lib/handlebars/compiler/ast.js
@@ -46,7 +46,6 @@ var AST = {
MustacheNode: function(rawParams, hash, open, strip, locInfo) {
LocationInfo.call(this, locInfo);
this.type = "mustache";
- this.hash = hash;
this.strip = strip;
// Open may be a string parsed from the parser or a passed boolean flag
@@ -58,6 +57,25 @@ var AST = {
this.escaped = !!open;
}
+ if (rawParams instanceof AST.SexprNode) {
+ this.sexpr = rawParams;
+ } else {
+ // Support old AST API
+ this.sexpr = new AST.SexprNode(rawParams, hash);
+ }
+
+ // Support old AST API that stored this info in MustacheNode
+ this.id = this.sexpr.id;
+ this.params = this.sexpr.params;
+ this.hash = this.sexpr.hash;
+ this.eligibleHelper = this.sexpr.eligibleHelper;
+ this.isHelper = this.sexpr.isHelper;
+ },
+
+ SexprNode: function(rawParams, hash) {
+ this.type = "sexpr";
+ this.hash = hash;
+
var id = this.id = rawParams[0];
var params = this.params = rawParams.slice(1);
@@ -84,8 +102,8 @@ var AST = {
},
BlockNode: function(mustache, program, inverse, close, locInfo) {
- if(mustache.id.original !== close.path.original) {
- throw new Exception(mustache.id.original + " doesn't match " + close.path.original);
+ if(mustache.sexpr.id.original !== close.path.original) {
+ throw new Exception(mustache.sexpr.id.original + " doesn't match " + close.path.original);
}
LocationInfo.call(this, locInfo);