CMS Development

Philip_Marsh
Contributor

Printing Multi-selector names

SOLVE

Hi,

 

I have a problem with printing from a multi-selector. I know this seems to be a common problem and I have tried multiple solutions but all produce the same result. 

 

I have set up in HubDB a column called Location Selector. All I want to do is print the values(Name) of each selection. 

 

When I try...

 

<ul class="list-con">
<li class="icon1">Location: {{ dynamic_page_hubdb_row.location_selector }}</li>
<li class="icon2">Employment: {{ dynamic_page_hubdb_row.employment }}</li>
</ul>

 

I get...

 

[{id=1, name='Cheadle', order=0}, {id=2, name='Manchester', order=1}]

 

As per other posts, I just want to post the name. If I use {{ dynamic_page_hubdb_row.location_selector.name }} I get nothing. It's blank. 

 

I even tried the below, which works, but prints out the full value as shown above.  

 

{% set table = hubdb_table_rows(tableID, queryparam) %}
{% for row in table %}
<ul class="list-con">
<li class="icon1">Location: {{ row["location_selector"] }}</li>
<li class="icon2">Employment: {{ dynamic_page_hubdb_row.employment }}</li>
</ul>
{% endfor %}

 

However, if I use row["location_selector"] .name I get the following warning. I don't get how it cannot resolve a property when it clearly shows name as a property. 

 

Warning:15:0 Cannot resolve property 'name' in '[{id=1, name='Cheadle', order=0}, {id=2, name='Manchester', order=1}]'

what am I missing?

0 Upvotes
1 Accepted solution
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

Printing Multi-selector names

SOLVE

Hi @Philip_Marsh,

 

A multi select field is stored as a list. So just like you need to loop through the rows of a HubDB table (or at least if you're not using a dynamic page), you'll also need to loop through the list items of a multi select field. And each selection is a dictionary. So if you're trying to print the names of each selection, you'd want to loop through the list and then access the name attribute within each loop. A simple version of that might look like:

 

{% for item in dynamic_page_hubdb_row.location_selector %}
  {{ item.name }}
{% endfor %}

In your case, I think you're more looking for something like this (I've also added code to make the names comma-separated):

 

<ul class="list-con">
  <li class="icon1">Location: {% for item in dynamic_page_hubdb_row.location_selector %}{{ item.name }}{% if not loop.last %},{% endif %} {% endfor %}</li>
  <li class="icon2">Employment: {{ dynamic_page_hubdb_row.employment }}</li>
</ul>

Does that help? If I can clarify anything, let me know.

 

Leland Scanlan

HubSpot Developer Support

View solution in original post

0 Upvotes
3 Replies 3
Philip_Marsh
Contributor

Printing Multi-selector names

SOLVE

Additional:

 

I can get .name to work if in Hubdb table I use a select, and not a multi-select. 

 

What the solution if I need it to be done using a multi-select

0 Upvotes
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

Printing Multi-selector names

SOLVE

Hi @Philip_Marsh,

 

A multi select field is stored as a list. So just like you need to loop through the rows of a HubDB table (or at least if you're not using a dynamic page), you'll also need to loop through the list items of a multi select field. And each selection is a dictionary. So if you're trying to print the names of each selection, you'd want to loop through the list and then access the name attribute within each loop. A simple version of that might look like:

 

{% for item in dynamic_page_hubdb_row.location_selector %}
  {{ item.name }}
{% endfor %}

In your case, I think you're more looking for something like this (I've also added code to make the names comma-separated):

 

<ul class="list-con">
  <li class="icon1">Location: {% for item in dynamic_page_hubdb_row.location_selector %}{{ item.name }}{% if not loop.last %},{% endif %} {% endfor %}</li>
  <li class="icon2">Employment: {{ dynamic_page_hubdb_row.employment }}</li>
</ul>

Does that help? If I can clarify anything, let me know.

 

Leland Scanlan

HubSpot Developer Support
0 Upvotes
Philip_Marsh
Contributor

Printing Multi-selector names

SOLVE

Thank you! 

 

I discovered item.*** yesterday too, and I think that may solve some of my other issues too

0 Upvotes