Skip to content

A func-y extension for Craft CMS, to enable anonymous function definition in Twig.

License

Notifications You must be signed in to change notification settings

TopShelfCraft/Funky

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Funky

A func-y little Craft CMS extension to enable anonymous function definition in Twig.

A (somewhat reluctant) Top Shelf Craft creation
Michael Rog, Proprietor

TL;DR.

Twig famously prohibits defining functions in templates and only parses arrow syntax in the context of filter parameters. Funky provides a workaround for this limitation, allowing you to create ad-hoc functions in your templates, using a special syntax.


Installation

Install the package via Composer.

composer require topshelfcraft/funky

The extension is bootstrapped automatically.

Usage

Once installed, Funky enables a special syntax for defining functions in your Twig templates: funky|fn()

You can create a function inline, by passing an arrow function as a solo argument:

funky|fn(x => x*2)

You can also register a function to access later, by passing both a name and the arrow function as arguments:

{% do funky|fn('double', x => x*2) %}
{% do funky|fn('triple', x => x*3) %}

{{ fns.double(42) }}
{{ fns.triple(42) }}

I have a bad feeling about this...

Well, it's a hack. If you're using this, it might be a code smell. Twig has resisted this functionality for good reason: Templates should only be concerned about presentation; ideally they should not contain logic.

Of course, once you understand the rules, you can break the rules...

Used thoughtfully, there are some circumstances in which the ability to define ad-hoc functions might actually help DRY up your template code:

  • Your custom functions are not reused across multiple templates, so it doesn't make sense to define them in a custom module.
  • You need to pass a custom function into a Collection pipeline method.
  • You're using Twig's filter, map, or reduce filters, and you want to reuse the same arrow function for multiple invocations in the same template.

So, proceed with temperance... or, reckless abandon; Who am I to tell you what to do?

How does it work?

Twig parses arrow function syntax in the context of filter parameters. So, Funky defines a fn filter, which either:

  • returns the Closure directly (if the first given argument is callable).
  • registers the function as a virtual property of the Funky class instance, making it available via the fns global (if the first given argument is a function name).

What are the system requirements?

Craft 4.0+ and PHP 8.0.2+

I found a bug.

For heaven's sake, really?!

Yes, I double-checked... It's a bug.

Very well. Please open a GitHub Issue. If you're feeling ambitious, submit a PR to the 4.x.dev branch.


Contributors:

About

A func-y extension for Craft CMS, to enable anonymous function definition in Twig.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages