Reputation: 425
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}
Upvotes: 0
Views: 1334
Reputation: 2623
Assuming items is an empty array, this should work:
{?items}
{#items}
...
{/items}
{:else}
There are no results
{/items}
Upvotes: 1
Reputation: 203
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}
Upvotes: 1