twig-lambda icon indicating copy to clipboard operation
twig-lambda copied to clipboard

Broken with Twig 2.10.0

Open jean-gui opened this issue 6 years ago • 7 comments

Twig 2.10.0 was recently released and it seems to be breaking this extension with the following message when calling the 'filter' filter :

Unexpected token "arrow function" of value "=>".

jean-gui avatar May 14 '19 15:05 jean-gui

Have the same - had to switch back to twig 2.9

crabnky avatar May 15 '19 15:05 crabnky

Same error with Twig 1.41.0 Downgrading to Twig 1.39.1 worked

DevManen avatar May 27 '19 13:05 DevManen

Same here. I had to downgrade twig from 2.11 to ~2.9

vialcollet avatar Jun 03 '19 10:06 vialcollet

I just realized that filter and map are included in Twig 2.10 and Twig 1.41 thus making this extension unnecessary in those cases.

jean-gui avatar Jun 04 '19 16:06 jean-gui

@jean-gui yeah, right - I use it for group_by functionality

crabnky avatar Jun 06 '19 09:06 crabnky

I could be wrong, but it seems like the main issue is that this extension's => token now conflicts with Twig's built-in => token. As a (hopefully temporary) workaround, I forked the repo and changed the token to ==>. This was line 22 and 28 of LambdaExtension.php:

public function getOperators()
    {
        return [
            [
-                '=>' => [
+               '==>' => [
                    'precedence' => 0,
                    'class' => '\DPolac\TwigLambda\NodeExpression\SimpleLambda'
                ],
            ],
            [
-                '=>' => [
+               '==>' => [
                    'precedence' => 0,
                    'class' => '\DPolac\TwigLambda\NodeExpression\LambdaWithArguments',
                    'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT
                ],
                ';' => [
                    'precedence' => 5,
                    'class' => '\DPolac\TwigLambda\NodeExpression\Arguments',
                    'associativity' => \Twig_ExpressionParser::OPERATOR_RIGHT
                ],
            ]
        ];
    }

I then updated my template code to use ==> instead of =>, i.e.:

{% set myVar = data|group_by(==> ...) %}

I'm not sure what other better option there is besides than changing twig-lambda's token.

gmhenderson avatar Jun 06 '19 17:06 gmhenderson

@gmhenderson, you are correct. I exended the LambdaExtension class and overrode the getOperators() method and it works again as expected.

blupointmedia avatar Aug 13 '19 14:08 blupointmedia