GithubHelp home page GithubHelp logo

timkelty / craft3-collections Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sjelfull/craft3-collections

0.0 2.0 0.0 32 KB

Clean up those complex templates with Laravel Collections

Home Page: https://superbig.co

License: MIT License

PHP 94.28% HTML 5.72%

craft3-collections's Introduction

Collections plugin for Craft CMS 3.x

Use Laravel Collections in Craft

Screenshot

Requirements

This plugin requires Craft CMS 3.0.0-beta.23 or later.

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
    
  2. Then tell Composer to load the plugin:

     composer require superbig/craft3-collections
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Collections.

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>

Brought to you by Superbig

craft3-collections's People

Contributors

sjelfull avatar brandonkelly avatar timkelty avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.