Using a dictionary to show content based on a contacts country

Hi!

I’m running into an issue with my email module.

This is the code:

{% set footer_data = {
 'The Netherlands': {
 'company_name': 'Company B.V.',
 'address': 'Street 200',
 'city': 'Amsterdam',
 'state': 'Noord-Holland',
 'zip_code': '2000',
 'country': 'The Netherlands'
 },
 'Germany': {
 'company_name': 'Company B.V.',
 'address': 'Street 200',
 'city': 'Emsbüren',
 'state': 'Niedersachsen',
 'zip_code': '2000',
 'country': 'Germany'
 },
 'France': {
 'company_name': 'Company B.V.',
 'address': 'Street 200',
 'city': 'Paris',
 'state': 'Île-de-France',
 'zip_code': '2000',
 'country': 'France'
 },
 'Belgium': {
 'company_name': 'Company B.V.',
 'address': 'Street 200',
 'city': 'Antwerpen',
 'state': 'Antwerpen',
 'zip_code': '2000',
 'country': 'Belgium'
 },
 'Denmark': {
 'company_name': 'Company B.V.',
 'address': 'Street 200',
 'city': 'København K',
 'state': 'Hovedstaden',
 'zip_code': '2000',
 'country': 'Denmark'
 },
 'Spain': {
 'company_name': 'Company B.V.',
 'address': 'Street 200',
 'city': 'Barcelona',
 'state': 'Barcelona',
 'zip_code': '2000',
 'country': 'Spain'
 },
 'United Kingdom': {
 'company_name': 'Company B.V.',
 'address': 'Street 200',
 'city': 'London',
 'state': 'London',
 'zip_code': '2000',
 'country': 'United Kingdom'
 }
} %}

{% set country = contact.country|string %}

<b>Footer TEST:</b><br>
Contact Country: {{ contact.country|pprint }}<br>
Dictionary Keys: {{ footer_data.keys() }}<br>
Country: {{ country|pprint }}<br>
Contact: {{ contact }}<br>
<b>Company name:</b>{{ footer_data[country].company_name }}

{% set countries = ['The Netherlands', 'Germany', 'France', 'Belgium', 'Denmark', 'Spain', 'United Kingdom'] %}

{% for c in countries %}
 {% if c == country %}
 <!-- Do stuff -->
 {% endif %}
{% endfor %}

I’m trying to show content from a dictionary based on the users country field. Problem is, when I try and take the variable containing the country STRING and place it in the code below:
{{ footer_data[country].company_name }}
It doesn’t show anything.

When I replace country with ‘Germany’ for example, so a literal string it does work… I’ve checked every bit. Using Pprint shows me that it’s a string. I’ve tried using “|string” but that doesn’t do anything. I even tried adding quotes myself but that doesn’t work either.

I’m not sure what’s wrong. Any help is greatly appreciated!
Thanks in advance!

Hi @DSantegoets,
Great question, thanks for asking the Community!
I found for you the solution from @miljkovicmisa on this similar post “get user geolocation” that might help you.
Also, if anybody else has anything to add and/or share, please feel free to join in the conversation :slightly_smiling_face:
Thanks and have a fantastic day!
Best,
Bérangère

Hi! Thanks for the reply but I don’t really see the solution to my problem in the post you mentioned.

What I’m basically asking is why a field containing a country in a string format cannot be used as a key when I try to match it with my keys in the dictionary?

So e.g. A key in my dictionary is “The Netherlands” as shown in my above code and I wanna match this key using:

{{ footer_data[country].company_name }}

So country being the users contact.country field as also shown in my above code. Which is (at least that is what Hubspot is telling me) also a string. When I print country with a user from The Netherlands it exactly matches with the key but for some reason it’s not showing the content I want, so in my above code that would be the company_name.

It does however, show when I literally use a string e.g.:

{{ footer_data['The Netherlands'].company_name }}

So I have the right data, it’s just not showing for whatever reason.

Thanks in advance!

Yours sincerely,

Dion

Hi @DSantegoets,
Thanks for the clarification and additional information!
I’d like to invite a couple of subject matter experts to this conversation: Hi @mangelet, @MBERARD and @Jnix284 do you have suggestions to help @DSantegoets, please?
Also, if anybody else has anything to add and/or share, please feel free to join in the conversation :slightly_smiling_face:
Thanks a lot :glowing_star: and have a nice day!
Best,
Bérangère

Hi @DSantegoets,

Sadly, I think you can’t work with contact values and HubL collections of values in emails du to server rendering.

however, maybe that solution is a better approach but never tried on my end…

Bests,

Matthieu