1

Here is the code:

{% if entry_name not in data and entry_total > 0 %}
    {% set data = data|merge({ (entry_name) : entry_total }) %}
{% else %}
    {% set data.entry_name = 10 %}
{% endif %}

Twig throws an error on the statement following else condition. Unexpected token "punctuation" of value "." ("end of statement block" expected). How can I set the value of a particular element of this associative array?

1 Answer 1

1

There's not a pretty way to do it in Twig other than what you've done in the if part of your conditional. Something like this should work:

{% if attribute(data, entry_name) is not defined and entry_total > 0 %}
    {% set data = data|merge({ (entry_name) : entry_total }) %}
{% else %}
    {% set data = data|merge({ (entry_name): 10 }) %}
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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