Support some way of checking array length
estan opened this issue · 2 comments
I know this may be frowned upon, but in the JS implementation you can do
{{#items.length}}
stuff rendered if items is non-empty
{{/items.length}}
Several other implementations have some similar way of checking this.
This is not really spec compliant, and I know Mustache is supposed to be logic-less, but it's very useful if you want to render a block only if an array is non empty. A prime example is if you want to render a HTML table, but only if it would contain any rows, then you could do:
{{#items.length}}
<h2>Items</h2>
<table>
<tbody>
{{#items}}
<tr><td>{{id}}</td><td>{{name}}</td></tr>
{{/items}}
</tbody>
</table>
{{/items.length}}
You don't want to render the enclosing <table><tbody>...</tbody></table>
if the table would be empty.
This is not possible without a .length
-type of feature, short of wrapping your list under an ugly dummy key, e.g:
data = { 'items_list' : { 'items' : ["Item 1", "Item 2", "etc"] } };
and then check for items_list
, or by introducing a has_items
alongside items
.
It would be very nice if qt-mustache
could support .length
or similar, for those of us who know we'll be using qt-mustache
and don't care too much about interoperability.
If there is a widely supported pattern across several common Mustache implementations I think I would be OK with supporting this. See #21 (comment) for notes on where to implement it.
Alright, as far as I know, there isn't really :/ I understand your reservations and will close this for now. Lets hope the Mustache spec is updated some day to include such a feature. For now I'll use a has_xyz
field.