Partials can be paths

Allows partials with slashes, a common partial syntax. For example:

    {{> shared/dude}}
This commit is contained in:
Les Hill
2012-12-13 10:33:20 -08:00
parent fd0560b951
commit 4bb794d814
8 changed files with 47 additions and 27 deletions
+9 -7
View File
@@ -33,13 +33,10 @@ var Handlebars = require('./base');
// pass or at runtime.
};
Handlebars.AST.PartialNode = function(id, context) {
this.type = "partial";
// TODO: disallow complex IDs
this.id = id;
this.context = context;
Handlebars.AST.PartialNode = function(partialName, context) {
this.type = "partial";
this.partialName = partialName;
this.context = context;
};
var verifyMatch = function(open, close) {
@@ -93,6 +90,11 @@ var Handlebars = require('./base');
this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
};
Handlebars.AST.PartialNameNode = function(name) {
this.type = "PARTIAL_NAME";
this.name = name;
};
Handlebars.AST.DataNode = function(id) {
this.type = "DATA";
this.id = id;
+2 -2
View File
@@ -160,7 +160,7 @@ Handlebars.JavaScriptCompiler = function() {};
},
partial: function(partial) {
var id = partial.id;
var partialName = partial.partialName;
this.usePartial = true;
if(partial.context) {
@@ -169,7 +169,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.opcode('push', 'depth0');
}
this.opcode('invokePartial', id.original);
this.opcode('invokePartial', partialName.name);
this.opcode('append');
},
+5 -1
View File
@@ -72,7 +72,7 @@ Handlebars.PrintVisitor.prototype.mustache = function(mustache) {
};
Handlebars.PrintVisitor.prototype.partial = function(partial) {
var content = this.accept(partial.id);
var content = this.accept(partial.partialName);
if(partial.context) { content = content + " " + this.accept(partial.context); }
return this.pad("{{> " + content + " }}");
};
@@ -111,6 +111,10 @@ Handlebars.PrintVisitor.prototype.ID = function(id) {
}
};
Handlebars.PrintVisitor.prototype.PARTIAL_NAME = function(partialName) {
return "PARTIAL:" + partialName.name;
};
Handlebars.PrintVisitor.prototype.DATA = function(data) {
return "@" + data.id;
};
+10 -2
View File
@@ -114,6 +114,10 @@ describe "Parser" do
"@#{id}"
end
def partial_name(name)
"PARTIAL:#{name}"
end
def path(*parts)
"PATH:#{parts.join("/")}"
end
@@ -218,11 +222,15 @@ describe "Parser" do
end
it "parses a partial" do
ast_for("{{> foo }}").should == root { partial id("foo") }
ast_for("{{> foo }}").should == root { partial partial_name("foo") }
end
it "parses a partial with context" do
ast_for("{{> foo bar}}").should == root { partial id("foo"), id("bar") }
ast_for("{{> foo bar}}").should == root { partial partial_name("foo"), id("bar") }
end
it "parses a partial with a complex name" do
ast_for("{{> shared/partial}}").should == root { partial partial_name("shared/partial") }
end
it "parses a comment" do
+3 -3
View File
@@ -521,11 +521,11 @@ test("GH-14: a partial preceding a selector", function() {
shouldCompileToWithPartials(string, [hash, {}, {dude:dude}], true, "Dudes: Jeepers Creepers", "Regular selectors can follow a partial");
});
test("Partials with literal paths", function() {
var string = "Dudes: {{> [dude]}}";
test("Partials with slash paths", function() {
var string = "Dudes: {{> shared/dude}}";
var dude = "{{name}}";
var hash = {name:"Jeepers", another_dude:"Creepers"};
shouldCompileToWithPartials(string, [hash, {}, {dude:dude}], true, "Dudes: Jeepers", "Partials can use literal paths");
shouldCompileToWithPartials(string, [hash, {}, {'shared/dude':dude}], true, "Dudes: Jeepers", "Partials can use literal paths");
});
suite("String literal parameters");
+8 -8
View File
@@ -132,24 +132,24 @@ describe "Tokenizer" do
result[4].should be_token("CONTENT", " baz")
end
it "tokenizes a partial as 'OPEN_PARTIAL ID CLOSE'" do
it "tokenizes a partial as 'OPEN_PARTIAL PARTIAL_NAME CLOSE'" do
result = tokenize("{{> foo}}")
result.should match_tokens(%w(OPEN_PARTIAL ID CLOSE))
result.should match_tokens(%w(OPEN_PARTIAL PARTIAL_NAME CLOSE))
end
it "tokenizes a partial with context as 'OPEN_PARTIAL ID ID CLOSE'" do
it "tokenizes a partial with context as 'OPEN_PARTIAL PARTIAL_NAME ID CLOSE'" do
result = tokenize("{{> foo bar }}")
result.should match_tokens(%w(OPEN_PARTIAL ID ID CLOSE))
result.should match_tokens(%w(OPEN_PARTIAL PARTIAL_NAME ID CLOSE))
end
it "tokenizes a partial without spaces as 'OPEN_PARTIAL ID CLOSE'" do
it "tokenizes a partial without spaces as 'OPEN_PARTIAL PARTIAL_NAME CLOSE'" do
result = tokenize("{{>foo}}")
result.should match_tokens(%w(OPEN_PARTIAL ID CLOSE))
result.should match_tokens(%w(OPEN_PARTIAL PARTIAL_NAME CLOSE))
end
it "tokenizes a partial space at the end as 'OPEN_PARTIAL ID CLOSE'" do
it "tokenizes a partial space at the end as 'OPEN_PARTIAL PARTIAL_NAME CLOSE'" do
result = tokenize("{{>foo }}")
result.should match_tokens(%w(OPEN_PARTIAL ID CLOSE))
result.should match_tokens(%w(OPEN_PARTIAL PARTIAL_NAME CLOSE))
end
it "tokenizes a comment as 'COMMENT'" do
+4 -2
View File
@@ -1,5 +1,5 @@
%x mu emu com
%x mu emu com par
%%
@@ -19,7 +19,7 @@
<com>[\s\S]*?"--}}" { yytext = yytext.substr(0, yyleng-4); this.popState(); return 'COMMENT'; }
<mu>"{{>" { return 'OPEN_PARTIAL'; }
<mu>"{{>" { this.begin("par"); return 'OPEN_PARTIAL'; }
<mu>"{{#" { return 'OPEN_BLOCK'; }
<mu>"{{/" { return 'OPEN_ENDBLOCK'; }
<mu>"{{^" { return 'OPEN_INVERSE'; }
@@ -46,6 +46,8 @@
<mu>[a-zA-Z0-9_$-]+/[=}\s\/.] { return 'ID'; }
<mu>'['[^\]]*']' { yytext = yytext.substr(1, yyleng-2); return 'ID'; }
<mu>. { return 'INVALID'; }
<par>\s+ { /*ignore whitespace*/ }
<par>[a-zA-Z0-9_$-/]+ { this.popState(); return 'PARTIAL_NAME'; }
<INITIAL,mu><<EOF>> { return 'EOF'; }
+6 -2
View File
@@ -45,8 +45,8 @@ mustache
partial
: OPEN_PARTIAL path CLOSE { $$ = new yy.PartialNode($2); }
| OPEN_PARTIAL path path CLOSE { $$ = new yy.PartialNode($2, $3); }
: OPEN_PARTIAL partialName CLOSE { $$ = new yy.PartialNode($2); }
| OPEN_PARTIAL partialName path CLOSE { $$ = new yy.PartialNode($2, $3); }
;
simpleInverse
@@ -91,6 +91,10 @@ hashSegment
| ID EQUALS DATA { $$ = [$1, new yy.DataNode($3)]; }
;
partialName
: PARTIAL_NAME { $$ = new yy.PartialNameNode($1); }
;
path
: pathSegments { $$ = new yy.IdNode($1); }
;