Unexpected token "punctuation" of value "." ("end of statement block" expected) I'm trying to build an array of values from 4 entries so I can lay them out in a comparison chart. I figured the best way to do this would be to add all of the values to a nested array. So I've set this up...

{% set plandata = {
        'membership-type' : {
            'title' : 'Membership Type',
            'url' : '',
            'price' : '',
            'items' : []
        }
    }
 %}


then I'm iterating through a field and trying to add them to the items portion like so...

{% for amenity in entry.availableAmenities %}
    {% set plandata.membership-type.items = plandata.membership-type.items|merge(amenity.title) %}
{% endfor %}


However I am getting a Template Error:  Unexpected token "punctuation" of value "." ("end of statement block" expected).

Can anyone give me any insight as to what I'm doing wrong?
I see three problems here.

One of them is the - in membership-type. plandata.membership-type does not mean the membership-type field of plandata, but more something like (plandata.membership) minus (type). So you will either want to use membershipType or membership_type, or the less readable twig attribute function: attribute(plandata, 'membership-type').

The second problem is that the twig merge filter operates on arrays, and you are passing it a string. This won't work: |merge(amenity.title), this will: |merge([amenity.title])

The third problem is that the twig set assigns values to variables, and plandata.membershipType.items is not a variable. The only way (in twig) to "assign" to a field of an array is with the merge filter. So you will need something like this:

{# create your amenityTitles array #}
{% set amenityTitles = [] %}
{% for amenity in entry.availableAmenities %}
  {% set amenityTitles = amenityTitles|merge([amenity.title]) %}
{% endfor %}

{# merge those amenityTitles with membershipType (somehow preexisting) #}
{% set membershipType = membershipType | merge({items: amenityTitles})}

{# merge that membershipType with plandata #}
{% set plandata = plandata | merge({membershipType: membershipType}) %}


If you can figure out a less deeply nested structure than that plandata you will probably be happier given the limitations of twig.