Skip to content
Merged
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
5 changes: 2 additions & 3 deletions src/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ function evalValue(str, scope) {
}

function isTruthy(val) {
if (val instanceof Array) return !!val.length;
return !!val;
return !isFalsy(val);
}

function isFalsy(val) {
return !isTruthy(val);
return false === val || undefined === val || null === val;;
}

module.exports = {
Expand Down
4 changes: 3 additions & 1 deletion tags/for.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ module.exports = function(liquid) {

render: function(scope, hash) {
var collection = Liquid.evalExp(this.collection, scope);
if (Liquid.isFalsy(collection)) {

if (!Array.isArray(collection) ||
(Array.isArray(collection) && collection.length === 0)) {
return liquid.renderer.renderTemplates(this.elseTemplates, scope);
}

Expand Down
11 changes: 9 additions & 2 deletions test/tags/for.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('tags/for', function() {
ctx = {
one: 1,
alpha: ['a', 'b', 'c'],
emptyArray: []
};
});
it('should support for', function() {
Expand All @@ -24,6 +25,12 @@ describe('tags/for', function() {
.to.be.rejectedWith(/tag .* not closed/);
});

it('should return else when for in empty array', function() {
var src = '{%for c in emptyArray%}a{%else%}b{%endfor%}';
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('b');
});

it('should support for else', function() {
var src = '{%for c in ""%}a{%else%}b{%endfor%}';
return expect(liquid.parseAndRender(src, ctx))
Expand Down Expand Up @@ -80,13 +87,13 @@ describe('tags/for', function() {
.to.eventually.equal('21');
});

it('should support for reversed in the middle position', function() {
it('should support for reversed in the first position', function() {
var src = '{% for i in (1..5) reversed limit:2 %}{{ i }}{% endfor %}';
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('21');
});

it('should support for reversed in the first position', function() {
it('should support for reversed in the middle position', function() {
Copy link
Owner

Choose a reason for hiding this comment

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

good catch

var src = '{% for i in (1..5) offset:2 reversed limit:4 %}{{ i }}{% endfor %}';
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('543');
Expand Down
8 changes: 7 additions & 1 deletion test/tags/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('tags/if', function() {
var ctx = {
one: 1,
two: 2,
emptyString: '',
emptyArray: []
};

Expand All @@ -19,7 +20,7 @@ describe('tags/if', function() {
it('should support if 2', function() {
var src = '{%if emptyArray%}a{%endif%}';
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('');
.to.eventually.equal('a');
});
it('should support if 3', function() {
var src = '{% if 2==3 %}yes{%else%}no{%endif%}';
Expand All @@ -46,4 +47,9 @@ describe('tags/if', function() {
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('');
});
it('should return true if empty string', function() {
var src = "{%if emptyString%}a{%endif%}";
return expect(liquid.parseAndRender(src, ctx))
.to.eventually.equal('a');
});
});
5 changes: 3 additions & 2 deletions test/tags/unless.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ describe('tags/unless', function() {
var liquid = Liquid();

it('should render else when predicate yields true', function() {
var src = '{% unless 1 %}yes{%else%}no{%endunless%}';
// 0 is truthy
var src = '{% unless 0 %}yes{%else%}no{%endunless%}';
return expect(liquid.parseAndRender(src))
.to.eventually.equal('no');
});
it('should render unless when predicate yields false', function() {
var src = '{% unless 0 %}yes{%else%}no{%endunless%}';
var src = '{% unless false %}yes{%else%}no{%endunless%}';
return expect(liquid.parseAndRender(src))
.to.eventually.equal('yes');
});
Expand Down