Permalink
Browse files

Change `empty` tag to `else`

closes gh-49
  • Loading branch information...
1 parent 2319405 commit 749bc656856e53665b3c09849d3898cd56e60e13 @paularmstrong paularmstrong committed Feb 12, 2012
Showing with 34 additions and 32 deletions.
  1. +1 −1 README.md
  2. +6 −8 docs/tags.md
  3. +18 −14 lib/tags.js
  4. +9 −9 tests/tags.test.js
View
@@ -34,7 +34,7 @@ Basic Example
<ul>
{% for author in authors %}
<li{% if loop.index <= 0 %} class="first"{% endif %}>{{ author }}</li>
- {% empty %}
+ {% else %}
<li>There are no authors.</li>
{% endfor %}
</ul>
View
@@ -9,10 +9,8 @@ Built-in Tags <a name="builtin" href="#builtin">#</a>
* [parent](#parent)
* [include](#include)
* [raw](#raw)
-* [for](#for)
-* [empty](#empty)
-* [if](#if)
-* [else (if)](#else)
+* [for](#for) - [else](#forelese)
+* [if](#if) - [else (if)](#else)
* [autoescape](#autoescape)
* [macro](#macro)
* [import](#import)
@@ -106,15 +104,15 @@ You can also apply filters to the object that you are iterating over.
The array `y` will first be reversed before looping over it.
{% endfor %}
-#### empty <a name="empty" href="#empty">#</a>
+#### else <a name="forelse" href="#forelse">#</a>
-For loops have a special tag available to them called `{% empty %}`.
+For loops have a special tag available to them called `{% else %}`.
-If the loop object is empty or `length === 0`, the content following the `empty` tag will be rendered.
+If the loop object is empty or `length === 0`, the content following the `else` tag will be rendered.
{% for person in people %}
{{ person }}
- {% empty %}
+ {% else %}
There are no people yet!
{% endfor %}
View
@@ -177,14 +177,26 @@ exports['if'] = function (indent, parentBlock) {
exports['if'].ends = true;
exports['else'] = function (indent, parentBlock) {
- if (_.last(this.parent).name !== 'if') {
- throw new Error('Cannot call else tag outside of "if" context.');
+ var last = _.last(this.parent).name,
+ thisArgs = _.clone(this.args),
+ ifarg,
+ args,
+ out;
+
+ if (last === 'for') {
+ if (thisArgs.length) {
+ throw new Error('"else" tag cannot accept arguments in the "for" context.');
+ }
+ return '} if (__loopLength === 0) {\n';
}
- var thisArgs = _.clone(this.args),
- ifarg = thisArgs.shift(),
- args = (parseIfArgs(thisArgs)),
- out = '';
+ if (last !== 'if') {
+ throw new Error('Cannot call else tag outside of "if" or "for" context.');
+ }
+
+ ifarg = thisArgs.shift();
+ args = (parseIfArgs(thisArgs));
+ out = '';
if (ifarg) {
out += '} else if (\n';
@@ -287,14 +299,6 @@ exports['for'] = function (indent, parentBlock) {
};
exports['for'].ends = true;
-exports.empty = function (indent) {
- if (_.last(this.parent).name !== 'for') {
- throw new Error('Cannot call "empty" tag outside of "for" context.');
- }
-
- return '} if (_.keys(__loopIter).length === 0) {\n';
-};
-
/**
* autoescape
* Special handling hardcoded into the parser to determine whether variable output should be escaped or not
View
@@ -244,7 +244,7 @@ exports['if'] = testCase({
test.strictEqual(tpl({ foo: [1] }), 'nope');
test.throws(function () {
- swig.compile('{% for i in foo %}hi!{% else %}nope{% endfor %}');
+ swig.compile('{% else %}');
}, Error, 'Cannot call else tag outside of "if" context.');
test.done();
},
@@ -357,17 +357,17 @@ exports['for'] = testCase({
test.done();
},
- empty: function (test) {
- var tpl = swig.compile('{% for foo in bar %}blah{% empty %}hooray!{% endfor %}');
- test.strictEqual(tpl({ bar: [] }), 'hooray!', 'empty in array');
- test.strictEqual(tpl({ bar: {}}), 'hooray!', 'empty in object');
+ else: function (test) {
+ var tpl = swig.compile('{% for foo in bar %}blah{% else %}hooray!{% endfor %}');
+ test.strictEqual(tpl({ bar: [] }), 'hooray!', 'else in array');
+ test.strictEqual(tpl({ bar: {}}), 'hooray!', 'else in object');
- test.strictEqual(tpl({ bar: [1] }), 'blah', 'not empty in array');
- test.strictEqual(tpl({ bar: { foo: 'foo' }}), 'blah', 'not empty in object');
+ test.strictEqual(tpl({ bar: [1] }), 'blah', 'not else in array');
+ test.strictEqual(tpl({ bar: { foo: 'foo' }}), 'blah', 'not else in object');
test.throws(function () {
- swig.compile('{% if foo %}hi!{% empty %}nope{% endif %}');
- }, Error, 'Cannot call "empty" tag outside of "for" context.');
+ swig.compile('{% for foo in bar %}hi!{% else if blah %}nope{% endfor %}');
+ }, Error, '"else" tag cannot accept arguments in the "for" context.');
test.done();
},

0 comments on commit 749bc65

Please sign in to comment.