Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support some way of checking array length #22

Closed
estan opened this issue Jan 5, 2016 · 2 comments
Closed

Support some way of checking array length #22

estan opened this issue Jan 5, 2016 · 2 comments

Comments

@estan
Copy link
Contributor

estan commented Jan 5, 2016

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.

@robertknight
Copy link
Owner

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.

@estan
Copy link
Contributor Author

estan commented Jan 5, 2016

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.

@estan estan closed this as completed Jan 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants