sparkartgroup / handlebars-helper

A collection of Handlebars Helpers for use with Solidus
MIT License
39 stars 9 forks source link

Add `where` helper #3

Closed Fauntleroy closed 11 years ago

Fauntleroy commented 11 years ago

The where helper should create a block that contains all of the items in an array that have a matching key/value. An optional limit can be provided that limits how many items are retrieved. A negative limit will traverse the array in reverse.

cats array

[{
    "name": "Wesley",
    "age": 8
},{
    "name": "Twizzler",
    "age": 8
},{
    "name": "Chip"
    "age": 1
}]

Handlebars

<!-- where with minimum arguments: cats age 1 -->
<ul>{{#where cats age 1}}<li>{{name}}</li>{{/where}}</ul>
<!-- where with limit: cats age 8 1 -->
<ul>{{#where cats age 8 1}}<li>{{name}}</li>{{/where}}</ul>
<!-- where with negative limit: cats age 8 -1 -->
<ul>{{#where cats age 8 -1}}<li>{{name}}</li>{{/where}}</ul>

Output

<!-- where with minimum arguments: cats age 1 -->
<ul>
    <li>Chip</li>
</ul>
<!-- where with limit: cats age 8 1 -->
<ul>
    <li>Wesley</li>
</ul>
<!-- where with negative limit: cats age 8 -1 -->
<ul>
    <li>Twizzler</li>
</ul>
Fauntleroy commented 11 years ago

Not sure about negative limits... maybe later.