Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
/ craft-closure Public archive

Allows you to use arrow function closures in Twig

License

Notifications You must be signed in to change notification settings

nystudio107/craft-closure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status

No Maintenance Intended

DEPRECATED

This Craft CMS module is no longer supported or maintained, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons.

Instead, use Twig 3.1.5's native functionality for arrow functions everywhere.

Closure for Craft CMS

Allows you to use arrow function closures in Twig

While Closure is a bit of a monkey patch, it's a pretty clean/simple one that relies on functionality that is already built into Twig

Requirements

Closure requires Craft CMS 4.x or 5.x

Installation

To install Closure, follow these steps:

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

     cd /path/to/project
    
  2. Then tell Composer to require the package:

     composer require nystudio107/craft-closure
    

About Closure

Twig supports arrow function closures, but only in the filter, map, and reduce filters.

Twig unfortunately has no plans to allow for more widespread usage of arrow function closures.

Craft Closure allows you to use arrow function closures anywhere, which is especially useful with Laravel Collection methods, many of which take a closure as a parameter.

Using Closure

Once you've added the nystudio107/craft-closure package to your project, no further setup is needed. This is because it operates as an auto-bootstrapping Yii2 Module.

You can then pass an arrow function closure as a parameter to anything that accepts them, such as many Laravel Collection methods:

    {% set collection = collect(['a', 'b', 'c']) %}
    {% set contains = collection.contains((value, key) => value == 'z') %}

Or you can assign an arrow function closure to a Twig variable for re-use:

    {% set collection = collect(['a', 'b', 'c']) %}
    {% set closure = (value, key) => value == 'a' %}
    {% set contains = collection.contains(closure) %}

Using arrow function closures especially useful now that Craft element queries can all return a Collection via the .collect() method.

More on Arrow Functions in Twig

More here: Twig Arrow Functions

Closure Roadmap

Some things to do, and ideas for potential features:

  • Initial release

Brought to you by nystudio107

About

Allows you to use arrow function closures in Twig

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages