Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw block helpers #573

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/handlebars/compiler/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ Handlebars.AST.PartialNode = function(partialName, context) {
this.context = context;
};

Handlebars.AST.RawBlockNode = function(mustache, content) {
this.type = "block";
this.mustache = mustache;
this.mustache.isHelper = true;
this.mustache.params.splice(0, 0, { type: "STRING", "string": content } );
};

Handlebars.AST.BlockNode = function(mustache, program, inverse, close) {
if(mustache.id.original !== close.original) {
throw new Handlebars.Exception(mustache.id.original + " doesn't match " + close.original);
Expand Down
2 changes: 1 addition & 1 deletion spec/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('blocks', function() {

equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
});

it("empty block", function() {
var string = "{{#goodbyes}}{{/goodbyes}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
Expand Down
20 changes: 20 additions & 0 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ describe('helpers', function() {
shouldCompileTo(string, [hash, helpers], "<a href='/root/goodbye'>Goodbye</a>");
});

it("helper for raw block gets raw content", function() {
var string = "{{{{raw}}}} {{test}} {{{{/raw}}}}";
var hash = { test: "hello" };
var helpers = { raw: function(content) {
return content;
} };
shouldCompileTo(string, [hash, helpers], " {{test}} ",
"raw block helper gets raw content");
});

it("helper for raw block gets parameters", function() {
var string = "{{{{raw 1 2 3}}}} {{test}} {{{{/raw}}}}";
var hash = { test: "hello" };
var helpers = { raw: function(content, a, b, c) {
return content + a + b + c;
} };
shouldCompileTo(string, [hash, helpers], " {{test}} 123",
"raw block helper gets raw content");
});

it("helper block with complex lookup expression", function() {
var string = "{{#goodbyes}}{{../name}}{{/goodbyes}}";
var hash = {name: "Alan"};
Expand Down
8 changes: 7 additions & 1 deletion src/handlebars.l
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

%x mu emu com
%x mu emu com raw

%%

Expand All @@ -18,8 +18,14 @@
return 'CONTENT';
}

<raw>"{{{{/"[^\s!"#%-,\.\/;->@\[-\^`\{-~]+/[=}\s\/.]"}}}}" { yytext = yytext.substr(5, yyleng-9); this.popState(); return 'END_RAW_BLOCK'; }
<raw>[^\x00]*?/("{{{{/") { return 'CONTENT'; }

<com>[\s\S]*?"--}}" { yytext = yytext.substr(0, yyleng-4); this.popState(); return 'COMMENT'; }

<mu>"{{{{" { return 'OPEN_RAW_BLOCK'; }
<mu>"}}}}" { this.popState(); this.begin('raw'); return 'CLOSE_RAW_BLOCK'; }
<mu>"{{{{"[^\x00]*"}}}}" { yytext = yytext.substr(4, yyleng-8); this.popState(); return 'RAW_BLOCK'; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this construct? I don't see it used in the productions below.

<mu>"{{>" { return 'OPEN_PARTIAL'; }
<mu>"{{#" { return 'OPEN_BLOCK'; }
<mu>"{{/" { return 'OPEN_ENDBLOCK'; }
Expand Down
7 changes: 6 additions & 1 deletion src/handlebars.yy
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ statements
;

statement
: openInverse program closeBlock { $$ = new yy.BlockNode($1, $2.inverse, $2, $3); }
: openRawBlock CONTENT END_RAW_BLOCK { $$ = new yy.RawBlockNode($1, $2); }
| openInverse program closeBlock { $$ = new yy.BlockNode($1, $2.inverse, $2, $3); }
| openBlock program closeBlock { $$ = new yy.BlockNode($1, $2, $2.inverse, $3); }
| mustache { $$ = $1; }
| partial { $$ = $1; }
| CONTENT { $$ = new yy.ContentNode($1); }
| COMMENT { $$ = new yy.CommentNode($1); }
;

openRawBlock
: OPEN_RAW_BLOCK inMustache CLOSE_RAW_BLOCK { $$ = new yy.MustacheNode($2[0], $2[1]); }
;

openBlock
: OPEN_BLOCK inMustache CLOSE { $$ = new yy.MustacheNode($2[0], $2[1]); }
;
Expand Down