0

I'm having issues trying to merge a nested hash to get unique values. I'm getting this error which points to the last line of for the loop:

A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "}" ("punctuation" expected with value ":").

{% set contentTypes = {} %}

{% for row in survey.questions %}
    {% set settings = row.settings.first %}
    {% set factor %}{{ settings.factor }}{{ settings.subFactor ? '_' ~ settings.subFactor }}{% endset %}
    {% set factorLbl = factor|title|replace('_', ' / ') %}
    {% set option %}{{ factor }}: { factorVal: '{{ factor }}', factorLbl: '{{ factorLbl }}'}{% endset %}
    {% set contentTypes = contentTypes|merge({option}) %}
{% endfor %}

Update

So factor is currently being set as a string, which is causing the merge to only set the last key in the for loop. How do I concatenate the two strings into a variable if settings.subFactor exists?

{% set factor = settings.factor ~ settings.subFactor ? '_' ~ settings.subFactor %}

3

You are setting option to a string here:

{% set option %}...{% endset %}

And merge takes an array or a hash as a parameter. It looks to me like you want option to be a hash:

{% set option = {factor: {'factorVal': factor, 'factorLbl': factorLbl}} %} 
|improve this answer|||||
  • Ahh, good catch. This gets rid of the error, however, the merge is not working as it only has one key. Adding {{ dump(option) }} within the for loop prints each key. I should have a total of 4 keys within the hash. – Siebird Dec 23 '15 at 13:45
0

Marion got me pointed in the right direction. I was setting factor as a string in my original post, which only allowed one key in the hash as it was 'seen' as the same every time through the merge function in the for loop. Converting factor to a variable fixed this:

{% set contentTypes = {} %}

{% for row in survey.questions %}
    {% set settings = row.settings.first %}
    {% set factor = settings.factor ~ (settings.subFactor ? '_' ~ settings.subFactor) %}
    {% set factorLbl = factor|title|replace('_', ' / ') %}
    {% set contentTypes = contentTypes|merge({ (factor) : {'value': factor, 'label': factorLbl} }) %}
{% endfor %}
|improve this answer|||||

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.