Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

logic - Mustache - How to detect array is not empty?

I want to implement the following logic with Mustache:

{{#if users.length > 0}}
    <ul>
        {{#users}}
            <li>{{.}}</li>
        {{/users}}
    </ul>
{{/if}}

// eg. data = { users: ['Tom', 'Jerry'] }

Should I modify the users structure to meet the need? For example:

{{#hasUsers}}
    <ul>
        {{#users}}
            <li>{{.}}</li>
        {{/users}}
    </ul>
{{/hasUsers}}

// eg. data = { hasUsers: true, users: ['Tom', 'Jerry'] }
question from:https://stackoverflow.com/questions/11653764/mustache-how-to-detect-array-is-not-empty

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sorry, this may be too late. But I had similar requirement and found a better way to do this:

{{#users.length}}
    <ul>
        {{#users}}
            <li>{{.}}</li>
        {{/users}}
    </ul>
{{/users.length}}
{{^users.length}}
    <p>No Users</p>
{{/users.length}}

Working sample here: http://jsfiddle.net/eSvdb/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...