ArrayHelper + twig

Hi, dear Yii2 users!

I am facing a weird issue with Yii2, Twig and ArrayHelper.

As for demonstration purposes, let’s say:

  • I have defined a function in my twig section in config/web.php called var_dump (that does exactly as it seems ;) )

  • My model [font="Courier New"]producer[/font] has a method [font="Courier New"]categories[/font] that returns an array of [font="Courier New"]Category[/font] object

So I have this in my view




{{ use ('yii\helpers\ArrayHelper') }}


{{ ArrayHelper.map(producer, 'id', 'name') | var_dump }}



That returns: [font="Courier New"]NULL[/font]

The only way I can have this working is defining a global in my Twig section’s config file this way:




                    'globals' => [

                        'array_helper' => '\yii\helpers\ArrayHelper',

                    ],



And this this works!




{{ array_helper.map(producer, 'id', 'name') | var_dump }}



Why? What if I don’t want to define a global but an “use” in my view?