HubDB selectrattr variable

Hi,

I was wondering if it was possible to use a variable when filtering Hubdb data so I wouldn’t have to create multiple row queries.
Here’s my setup.

{% set data = hubdb_table_rows(xxxxxx) %}
.

{% for country in countries %}

{% set overview = data|selectattr(‘country’,‘equalto’, country) %}
.

{% endfor %}

{% endfor %}

So, is there some kind of a trick to use variables in this situation. Thanks!

Hi @sanntn :waving_hand: Thanks for reaching out. Let’s see if we can get this conversation started — @alyssamwilie @Kevin-C @LMeert do you have any experience or insight here?

Thank you! — Jaycee

Hey @sanntn

Thanks @Jaycee_Lewis

You’ll want to review this documentation and the operators docs, but the implementation is rather simple:

{% set country = "Armenia" %}
{% set country_query = "name__eq=" ~ country %}
{% set data = hubdb_table_rows(5424505) %}
{% set select_data = data|selectattr("name","eq",country) %}
{{ country }}<hr>
{% for item in select_data %}
 {{ item.name }} - {{ item.gdp }}<br>
{% endfor %}

or

{% set country = "Armenia" %}
{% set country_query = "name__eq=" ~ country %}
{% set data = hubdb_table_rows(5424505, country_query) %}
{{ country }}<hr>
{% for item in data %}
 {{ item.name }} - {{ item.gdp }}<br>
{% endfor %}

The output:

KevinC_0-1655503858312.png

Hope this gets you moving!

I’m trying ti figure out why this solution is not working for me.

If i use a value definition for country, like in the example, with a defined string as value, it works. However, when I assign the value for the country variable from another variable (a contact property value in my case) it fails and the selectattr returns zero results.

so

{% set country=“Armenia” %} works

but

{% set country= contact[‘country’] %}

will not result in a match, however, echoing both {{ country}} and {{contact[‘country’] }} shows me Armenia twice.

any ideas why this happens?

Hey @PDeT

Comparing strings can be tricky! Look into using a few filters to clean up the strings comparing. Potential filters being; trim, lower, replace, and regex_replace.

Edit:

I would also check that the user is cookied before banging your head against the wall too many times!

Hi Kevin, thx for tuning in!

Even if i set the value to match to equal the table row value, it does not find a match:

{% set rows = hubdb_table_rows(XXXX) %}

{% set test = hubdb_table_row(XXXX,YYYY) %}
{% set queryValue = test.title %}

{% set match = rows|selectattr(‘title’,‘eq’, queryValue ) %}

{{ match }}

renders: []

if test.title is not going to match with any row title field from the table it was taken, then there is something really wrong with selectattr. My guess is that there is something wrong with the parser, when the variable value is not known at compile time.

forgot to mention @Kevin-C