<p>Twig didn’t add arrow function support until v2.10.0, so you need to be on at least Craft 3.1.29 if you want to use them.</p>

CC BY-SA 4.0
2019-06-21T16:33:37.473Z
2019-06-21T16:33:37.473Z
Brandon Kelly
<p>If you want to use arrow functions everywhere, so you can assign them to variables for re-use, or pass them to Collection methods that support callbacks, you'll need Craft Closure:</p>
<p><a href="https://github.com/nystudio107/craft-closure" rel="nofollow noreferrer">https://github.com/nystudio107/craft-closure</a></p>
<p>More here: <a href="https://craftquest.io/courses/arrow-functions-in-twig" rel="nofollow noreferrer">Twig Arrow Functions</a></p>

CC BY-SA 4.0
2022-08-25T18:18:45.340Z
2022-08-25T18:18:45.340Z
andrew.welch
<p><br/>
I tried to use reduce function in template, but got error. It seems twig/craftcms can not work with such functions.
Reduce function docs: <a href="https://docs.craftcms.com/v3/dev/filters.html#reduce" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/filters.html#reduce</a></p>

<p>My example:</p>

<pre><code>{% set data = craft.entries.section("faqChannel").orderBy('faqOrder').all() %}
{% set types = data|reduce((acc, v) =&gt; v.faqType is not in acc ? acc|merge([v.faqType]) : acc, []) %}
</code></pre>

<p>Error:<br/>
<code>Twig Syntax Error – Twig_Error_Syntax</code><br/>
<code>An opened parenthesis is not properly closed. Unexpected token "punctuation" of value "," ("punctuation" expected with value ")").</code></p>

CC BY-SA 4.0
2019-06-20T20:55:53.327Z
This is likely a bug in Craft - can you share this on https://github.com/craftcms/cms/issues so we can look into it?
Brad Bell
CC BY-SA 4.0
2019-06-21T06:07:03.053Z
@BradBell Done: https://github.com/craftcms/cms/issues/4426
g1un
<filter><filtering>
Arrow functions in twig or craft cms
# Arrow functions in twig or craft cms

(Asked by: username_0 on 2019-06-20)

I tried to use reduce function in template, but got error. It seems twig/craftcms can not work with such functions.
Reduce function docs: https://docs.craftcms.com/v3/dev/filters.html#reduce
My example:
{% set data = craft.entries.section("faqChannel").orderBy('faqOrder').all() %}
{% set types = data|reduce((acc, v) => v.faqType is not in acc ? acc|merge([v.faqType]) : acc, []) %}

Error:
Twig Syntax Error – Twig_Error_Syntax
An opened parenthesis is not properly closed. Unexpected token "punctuation" of value "," ("punctuation" expected with value ")").

**Tags:** filter, filtering
> **Comments:**
> **username_1**: This is likely a bug in Craft - can you share this on https://github.com/craftcms/cms/issues so we can look into it?  
> **username_0**: @BradBell Done: https://github.com/craftcms/cms/issues/4426  

---

## 2 answers

### Answer by username_2 on 2019-06-21. Score: 1

Twig didn’t add arrow function support until v2.10.0, so you need to be on at least Craft 3.1.29 if you want to use them.


---

### Answer by username_3 on 2022-08-25. Score: 1

If you want to use arrow functions everywhere, so you can assign them to variables for re-use, or pass them to Collection methods that support callbacks, you'll need Craft Closure:

<https://github.com/nystudio107/craft-closure>

More here: [Twig Arrow Functions](https://craftquest.io/courses/arrow-functions-in-twig)


---

