Skip to content

Commit

Permalink
✨ 🎨 Allow foreach else inside of get helper
Browse files Browse the repository at this point in the history
closes #7242

- before this, the get helper's else was used for empty resultsets
- the argument was made that we should fall through to a foreach or with helper's else instead
- I agree that this is the more natural, consistent approach, and so would like to change it for Ghost 1.0

E.g. as of this PR we now have:

{{#get "posts" filter="tag:doesnt-exist"}}
  {{#foreach posts}}
  {{else}}
    this ges executed because there are no results
  {{/foreach}}
{{/get}}

instead of

{{#get "posts" filter="tag:doesnt-exist"}}
  {{#foreach posts}}
  {{else}}
  {{/foreach}}
{{each}}
    this ges executed because there are no results
{{/get}}
  • Loading branch information
ErisDS committed Mar 14, 2017
1 parent f4a68a2 commit d249c92
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
5 changes: 0 additions & 5 deletions core/server/helpers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ get = function get(resource, options) {
return apiMethod(apiOptions).then(function success(result) {
var blockParams;

// If no result is found, call the inverse or `{{else}}` function
if (_.isEmpty(result[resource])) {
return options.inverse(self, {data: data});
}

// block params allows the theme developer to name the data using something like
// `{{#get "posts" as |result pageInfo|}}`
blockParams = [result[resource]];
Expand Down
8 changes: 4 additions & 4 deletions core/test/unit/server_helpers/get_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ describe('{{#get}} helper', function () {
'posts',
{hash: {filter: 'tags:none'}, fn: fn, inverse: inverse}
).then(function () {
fn.called.should.be.false();
inverse.calledOnce.should.be.true();
inverse.firstCall.args[1].should.be.an.Object().and.have.property('data');
inverse.firstCall.args[1].data.should.be.an.Object().and.not.have.property('error');
fn.calledOnce.should.be.true();
fn.firstCall.args[0].should.be.an.Object().with.property('posts');
fn.firstCall.args[0].posts.should.have.lengthOf(0);
inverse.called.should.be.false();

done();
}).catch(done);
Expand Down

0 comments on commit d249c92

Please sign in to comment.