1

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?

1

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.

  • Well here's what I'm trying to do. This is an existing site I just got assigned to work on not knowing anything about Craft or twig There are 4 entries, a main "Membership" page that then has a list of amenities assigned to it. Then 3 entries that are the 3 different membership descriptions, each of these entries has amenities assigned to them. I am attempting to build a comparison chart with the main page in column 1, then checkboxes for matching amenities for the 3 membership columns. I figured iterating through the amenities and add them to arrays to kick them out would be best. Opinion? – user2040962 Feb 21 '18 at 20:02
  • Maybe just iterating through the amenities and kick them out, or iterate through them and use {% set something %} ... {% endset %} to capture the html and then {{something}} later to display it. You can create complex datastructures they way you are trying to in twig, but it is a total pain in the ass. – Marion Newlevant Feb 21 '18 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.