CMS Development

Daniel_eon
Participant

Updating key and value in a dict with variables

SOLVE

Hi everyone,

 

I am trying to update a dict variable with both key and value through variables. While the value variable works, the key variable just prints the variable name instead of the variable content.

 

 

 

      {% set test_key = 'Key-Name' %}
      {% set test_value = 'Value-string' %}
      {% set test_object = {} %}
      {% do test_object.update ({ "{{ test_key }}" : test_value }) %}
      {{ test_object }}

 

 

I also tried:

 

{% do test_object.update ({ '{{ test_key }}' : test_value }) %}
{% do test_object.update ({ test_key : test_value }) %}

 

 

No method worked.

 

I looked at the solution proposed at https://community.hubspot.com/t5/CMS-Development/Is-there-a-way-to-create-a-dictionary-with-dynamic-... but as far as I understand, this method grabs a key from another dict and adds it to a new dict. However, my key variable is just a string and not part of another dict.

 

Any advice?

 

Thanks!

0 Upvotes
1 Accepted solution
Daniel_eon
Solution
Participant

Updating key and value in a dict with variables

SOLVE

Actually, the solution in the other post works! I just didn't understand it correctly. 

This code works and sets both the key and the value dynamically:

 

      {% set test_key = 'Key-Name' %}
      {% set test_value = 'Value-string' %}
      {% set test_object = {} %}
      {% do test_object.put (test_key, test_value) %}
      {{ test_object }}

 

View solution in original post

1 Reply 1
Daniel_eon
Solution
Participant

Updating key and value in a dict with variables

SOLVE

Actually, the solution in the other post works! I just didn't understand it correctly. 

This code works and sets both the key and the value dynamically:

 

      {% set test_key = 'Key-Name' %}
      {% set test_value = 'Value-string' %}
      {% set test_object = {} %}
      {% do test_object.put (test_key, test_value) %}
      {{ test_object }}