質問

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}
役に立ちましたか?

解決

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}

他のヒント

Assuming items is an empty array, this should work:

{?items}
    {#items}
        ...
    {/items}    
{:else}
    There are no results
{/items}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top