0

I'm looking for a clever way in dust.js to determine if an array of objects (let's say items) has at least one item preferably without using @if or looping through every item

It would be nice if this worked, but it doesn't :(

{@size key=items}
    {@gt value=0}
        asdf
    {/gt}
{/size}
1
  • It's actually an empty object, not an array but still having the issue Commented Jan 17, 2014 at 15:45

2 Answers 2

1

I've created an issue with the same requirement. Typically you should create an alternative helper which should accept inner blocks, and this helper will internally call @size helper.

Let's name it @sizeOf:

dust.helpers.sizeOf = function(chunk, context, bodies, params) {
  var value = this.size(chunk, context, bodies, params);
  return (bodies && bodies.block) ? chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, selectKey: value })) : value;
};

And we should use it like this:

{@sizeOf key=items}
  {@gt value=0}
    asdf
  {/gt}
{/size}
Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome, I didn't think of a dust helper. I ended up using explicit arrays at the data level so I can use the simple {?data}{/data} check
1

Assuming items is an empty array, this should work:

{?items}
    {#items}
        ...
    {/items}    
{:else}
    There are no results
{/items}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.