diff --git a/compiler/parse/state/mustache.js b/compiler/parse/state/mustache.js index b27e694608..98d371b418 100644 --- a/compiler/parse/state/mustache.js +++ b/compiler/parse/state/mustache.js @@ -112,7 +112,9 @@ export default function mustache ( parser ) { else if ( parser.eat( 'else' ) ) { const block = parser.current(); - if ( block.type !== 'IfBlock' ) parser.error( 'Cannot have an {{else}} block outside an {{#if ...}} block' ); + if ( block.type !== 'IfBlock' && block.type !== 'EachBlock' ) { + parser.error( 'Cannot have an {{else}} block outside an {{#if ...}} or {{#each ...}} block' ); + } parser.allowWhitespace(); parser.eat( '}}', true ); diff --git a/test/parser/each-block-else/input.html b/test/parser/each-block-else/input.html new file mode 100644 index 0000000000..59925e8db7 --- /dev/null +++ b/test/parser/each-block-else/input.html @@ -0,0 +1,5 @@ +{{#each animals as animal}} +

{{animal}}

+{{else}} +

no animals

+{{/each}} diff --git a/test/parser/each-block-else/output.json b/test/parser/each-block-else/output.json new file mode 100644 index 0000000000..8d82d5a677 --- /dev/null +++ b/test/parser/each-block-else/output.json @@ -0,0 +1,67 @@ +{ + "html": { + "start": 0, + "end": 84, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 84, + "type": "EachBlock", + "expression": { + "start": 8, + "end": 15, + "type": "Identifier", + "name": "animals" + }, + "context": "animal", + "children": [ + { + "start": 29, + "end": 46, + "type": "Element", + "name": "p", + "attributes": [], + "children": [ + { + "start": 32, + "end": 42, + "type": "MustacheTag", + "expression": { + "start": 34, + "end": 40, + "type": "Identifier", + "name": "animal" + } + } + ] + } + ], + "else": { + "children": [ + { + "attributes": [], + "children": [ + { + "data": "no animals", + "end": 70, + "start": 60, + "type": "Text" + } + ], + "end": 74, + "name": "p", + "start": 57, + "type": "Element" + } + ], + "end": 75, + "start": 55, + "type": "ElseBlock" + } + } + ] + }, + "css": null, + "js": null +}