-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trivial arrow filter #3677
Comments
Related:
I think the |
I don't think this filter belongs to the core. In most cases, the same result can be achieved by using a Twig expression directly. |
But then we can't chain it up with other filters easily. {{ items
|filter(item => ..)
|map(item => ..)
|join(', ')
|arrow(markup => (markup|trim is not empty) ? _self.wrap('div'))
}} Or to call a macro from apply: {% set title %}
<h3>Hello</h3>
{% endset %}
{% apply arrow(markup => (markup|trim is not empty) ? title ~ markup : '') %}
{{ output }}
{% endapply %} (btw the |
To make this work this one line: https://github.com/twigphp/Twig/blob/3.x/src/ExpressionParser.php#L483 from: foreach ($this->parseArguments() as $n) { to: foreach ($this->parseArguments(false, false, true) as $n) { then closures all work in the context we want, e.g.: {% set collection = collect(['a', 'b', 'c']) %}
{% set contains = collection.contains((value, key) => value == 'a') %} with a nice, clean, normal syntax. I'd love to see this happen, too. |
+1 for this. As a consumer of Twig, I just want it to work as it looks like it should. And it looks like it should support closures. Why does it matter if there are other ways of doing it? |
This is a solution for Craft CMS users: https://github.com/nystudio107/craft-closure |
+1 from me on this too! The change proposed by @khalwat seems clean! |
Here, the comments are talking about something different from the ticket description. The description talks about having an |
Closing here and reopening #3192 |
Hello @fabpot
Indeed the comments are somewhat off track, I don't really see how they relate to the original request. For me, the original request is still valid, it would provide more possibilities in filters and pipe expressions, which would otherwise require cluttered code blocks. |
There are a few twig filters that allow arrow functions as argument:
map
,filter
,reduce
.All of these apply the arrow function to array values (or keys).
I wonder, could we make a trivial filter that applies the arrow function to the original piped argument?
The main purpose would be as part of a longer pipe chain.
I noticed this can already be done in userland, but it seems like a useful addition to core, or not?
The text was updated successfully, but these errors were encountered: