Collections

Clean up those complex templates with Laravel Collections

Installation #

To install Collections, follow these steps:

  1. Download & unzip the file and place the collections directory into your craft/plugins directory
  2. Install plugin in the Craft Control Panel under Settings > Plugins
  3. The plugin folder should be named collections for Craft to see it.

Collections works on Craft 2.4.x and Craft 2.5.x.

Collections Overview #

Here is some good inspiration on what you can do with Collections:

Configuring Collections #

Add your macros to the config file:

<?php
return [

    /** Add your macros here
     * "macros" => [
     *     'toUpper' => function () {
     *         return $this->map(function ($value) {
     *             return strtoupper($value);
     *         });
     *     },
     * ],
     *
     */

    "macros" => [

    ],

];

Using Collections #

Group tags by letter: #

Add this macro to your config:

<?php
return [
    'macros' => [
        'tagGroups' => function () {
            return $this->groupBy(function ($tag) {
                return substr($tag->title, 0, 1);
            });
        }
    ],
];
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.1.2/css/bulma.css">
</head>
<body>

<div class="section hero is-primary">
    <div class="hero-body">
        <div class="container">
            <h1 class="title">Tags</h1>
            <p class="subtitle">
                Every tag on the site.
            </p>
        </div>
    </div>
</div>

<h2>Tag groups</h2>

<div class="section">
    <div class="container">
        <ul class="has-columns has-text-centered">
            {% set collection = craft.tags.group('media') | collect %}
            {% for letter, tags in collection.tagGroups() %}
                <div class="letter-group">
                    <h3 class="title is-1 letter">{{ letter }}</h3>

                    <ul>
                        {% for tag in tags %}
                            <li class="title is-5">
                                <a href="/tags/{{ tag.slug }}">{{ tag.title }}</a>
                            </li>
                        {% endfor %}
                    </ul>
                </div>
            {% endfor %}
        </ul>
    </div>
</div>

</body>
</html>

Releases

1.0.0 — 2017-12-06 Download
  • Added: Initial release

Currently available for Craft consulting and custom plugin development

Contact me