diff options
author | kpdecker <kpdecker@gmail.com> | 2014-10-27 00:23:32 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2014-10-27 19:17:16 -0500 |
commit | 4282668d47b90da0d00cf4c4a86977f18fc8cde4 (patch) | |
tree | a7254e549e27afb8043b03dd6ddd21e3afaae2e6 /spec/parser.js | |
parent | 8aa2a34a8d2453a45a3bc9d45e1fbed17e4d8bbc (diff) | |
download | handlebars.js-4282668d47b90da0d00cf4c4a86977f18fc8cde4.zip handlebars.js-4282668d47b90da0d00cf4c4a86977f18fc8cde4.tar.gz handlebars.js-4282668d47b90da0d00cf4c4a86977f18fc8cde4.tar.bz2 |
Implement parser for else chaining of helpers
Allows users to chain multiple helpers together using their inverse callbacks. I.e.
```
{{#if foo}}
{{else if bar}}
{{else}}
{{/if}}
```
The control flow here effectively causes the helpers to be nested. The above is actually syntactic sugar for this:
```
{{#if foo}}
{{else}}
{{#if bar}}
{{else}}
{{/if}}
{{/if}}
```
Any helper may be used in this manner, the only requirement is they support normal calls and inverse calls.
Introduces a breaking change in that `{{else foo}}` may no longer be used as a root level operator. Instead `{{^foo}}` must be used.
Fixes #72.
Diffstat (limited to 'spec/parser.js')
-rw-r--r-- | spec/parser.js | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/spec/parser.js b/spec/parser.js index 131160a..3686363 100644 --- a/spec/parser.js +++ b/spec/parser.js @@ -116,6 +116,10 @@ describe('parser', function() { equals(ast_for("{{#foo}} bar {{else}} baz {{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n CONTENT[ ' baz ' ]\n"); }); + it('parses multiple inverse sections', function() { + equals(ast_for("{{#foo}} bar {{else if bar}}{{else}} baz {{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n CONTENT[ ' bar ' ]\n {{^}}\n BLOCK:\n {{ ID:if [ID:bar] }}\n PROGRAM:\n {{^}}\n CONTENT[ ' baz ' ]\n"); + }); + it('parses empty blocks', function() { equals(ast_for("{{#foo}}{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n PROGRAM:\n"); }); @@ -147,8 +151,10 @@ describe('parser', function() { it('parses a standalone inverse section', function() { equals(ast_for("{{^foo}}bar{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n {{^}}\n CONTENT[ 'bar' ]\n"); }); - it('parses a standalone inverse section', function() { - equals(ast_for("{{else foo}}bar{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n {{^}}\n CONTENT[ 'bar' ]\n"); + it('throws on old inverse section', function() { + shouldThrow(function() { + equals(ast_for("{{else foo}}bar{{/foo}}"), "BLOCK:\n {{ ID:foo [] }}\n {{^}}\n CONTENT[ 'bar' ]\n"); + }, Error); }); it("raises if there's a Parse error", function() { |