Lloydinator
Lloydinator

Reputation: 303

Add Expression to Value of Array in Twig

I can't wrap my head around this. I'm on mobile so forgive me. I'm trying to add dynamic values to an array like this:

{%  set myArray = myArray|merge("href": {{ product.href }})%}.

I'm getting the 'A hash key must be a quotes string, a number, a name...'.

What am I doing wrong?

Upvotes: 1

Views: 182

Answers (1)

pedram shabani
pedram shabani

Reputation: 1680

You have syntax error .

It is wrong

{%  set myArray = myArray|merge("href": {{ product.href }})%}.

Use this code instead

   {%  set myArray = myArray|merge({ 'href': product.href } ) %}.

The merge filter works on hashes. For hashes, the merging process occurs on the keys: if the key does not already exist, it is added but if the key already exists, its value is overridden.

Upvotes: 1

Related Questions