├── LICENSE.txt ├── README.md └── group-by-array.html /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Max White 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Jekyll Group-By-Array 2 | ===================== 3 | 4 | A liquid include file for Jekyll that allows an object to be grouped by an array. For instance, it could be used to list posts by tag. 5 | 6 | Usage 7 | ----- 8 | 9 | Copy the [`group-by-array.html`](https://raw.githubusercontent.com/mushishi78/jekyll-group-by-array/master/group-by-array.html) file into your Jekyll project's `_includes` directory. 10 | 11 | Include the file in your template, passing in a `collection` and a `field` variable. Then Jekyll Group-By-Array will set `group-names` and `group-items` global variables with the results. 12 | 13 | For example: 14 | 15 | ``` liquid 16 | --- 17 | layout: page 18 | title: Tags 19 | permalink: /tags/ 20 | --- 21 | 22 | {% include group-by-array.html collection=site.posts field='tags' %} 23 | 24 | 40 | ``` 41 | 42 | For an extended example, checkout out this [group-by-array example branch](https://github.com/mushishi78/fresh-jekyll/tree/jekyll-group-by-array) of the Fresh Jekyll repository. 43 | 44 | Contributing 45 | ------------ 46 | 47 | 1. [Fork it](https://github.com/mushishi78/jekyll-group-by-array/fork) 48 | 2. Create your feature branch (`git checkout -b my-new-feature`) 49 | 3. Commit your changes (`git commit -am 'Add some feature'`) 50 | 4. Push to the branch (`git push origin my-new-feature`) 51 | 5. Create a new Pull Request -------------------------------------------------------------------------------- /group-by-array.html: -------------------------------------------------------------------------------- 1 | {% comment %} 2 | # Jekyll Group-By-Array 0.1.0 3 | # https://github.com/mushishi78/jekyll-group-by-array 4 | # © 2015 Max White 5 | # MIT License 6 | {% endcomment %} 7 | 8 | {% comment %} Initialize {% endcomment %} 9 | {% assign __empty_array = '' | split: ',' %} 10 | {% assign group_names = __empty_array %} 11 | {% assign group_items = __empty_array %} 12 | 13 | {% comment %} Map {% endcomment %} 14 | {% assign __names = include.collection | map: include.field %} 15 | 16 | {% comment %} Flatten {% endcomment %} 17 | {% assign __names = __names | join: ',' | join: ',' | split: ',' %} 18 | 19 | {% comment %} Uniq {% endcomment %} 20 | {% assign __names = __names | sort %} 21 | {% for name in __names | sort %} 22 | 23 | {% comment %} If not equal to previous then it must be unique as sorted {% endcomment %} 24 | {% unless name == previous %} 25 | 26 | {% comment %} Push to group_names {% endcomment %} 27 | {% assign group_names = group_names | push: name %} 28 | {% endunless %} 29 | 30 | {% assign previous = name %} 31 | {% endfor %} 32 | 33 | 34 | {% comment %} group_items {% endcomment %} 35 | {% for name in group_names %} 36 | 37 | {% comment %} Collect if contains {% endcomment %} 38 | {% assign __item = __empty_array %} 39 | {% for __element in include.collection %} 40 | {% if __element[include.field] contains name %} 41 | {% assign __item = __item | push: __element %} 42 | {% endif %} 43 | {% endfor %} 44 | 45 | {% comment %} Push to group_items {% endcomment %} 46 | {% assign group_items = group_items | push: __item %} 47 | {% endfor %} 48 | --------------------------------------------------------------------------------