Dict variables and updating them


Hi there,

I'm still relatively new to hubspot and am working my way through implementing an existing site onto the platform.

I need to create a relatively complex dictionary of data, that'll be updated further into the page template. So, i'm working through some simple examples to make sure I know hubl can do what I need.

{% set dict_var = {'name': 'Item Name', 'price': '$20', 'size':'XL'} %}
{% set dict_var['location'] = 'fancy location' %}

The above is one example of what I'd like to do. I.e., create a dict - and then update it on the next line. However - this simply won't work. If I print this variable on to the page (further down) it only shows the data without the location key.

I've tried various other ways of attempting to adjust the dict to no avail. Help?


Hi @cadriel, it doesn't look like this method is documented on our end, but since HubL is based loosely off of jinja2 which is a subset of python (more info on dicts in python here: https://www.digitalocean.com/community/tutorials/understanding-dictionaries-in-python-3), you can use the update method on the dictionary. The way I updated this dictionary is via the following code:

{% set dict_var = {'name': 'Item Name', 'price': '$20', 'size':'XL'} %}
{% set test = dict_var.update({'7': 'seven'}) %}
{% set test = dict_var.update({'8': 'eight'}) %}
{{ dict_var }}

Printing dict_var on that last line above ends up including both the 7 and 8 properties, and the whole dictionary is printed out. Here's an example --> http://blog.playtimepottery.net/macros


You're a legend. Thanks @Connor_Barley.

Are there other generic docs around I could refer to in cases like this? I did refer to some python docs before creating this post but it didn't mention the update method. Perhaps the Jinja docs? Any specific versions?


Hey @cadriel, I usually take a look at these docs: http://jinja.pocoo.org/docs/2.10/ and I really like these ones on Python: https://www.programiz.com/python-programming/methods/dictionary/update


The simple answer is

def setVar(self, var):
    for key, value in var.iteritems():
        setattr(self, key, value)

Or you can refer this
For learning python: Visit