Checking for the presence of a value in an array (list_membership)

SOLVE
Highlighted
Regular Contributor

Hi there,

 

I'm looking for a way to check if the site visitor has signed up on a particular form. The best way I've found is to look at the "list_membership" variable. This variable consists of an array of numbers that represent lists. 

 

I thought it would be pretty straightforward to check for the presence of a number in an array, but alas, I haven't had any success yet. 

This is how Hubspot recommends that you check to see if a tag is present on a blog post:

{% if 'my tag' in post.topic_list|map('name') %}

 

 

Blog topics are in an object, and it appears that the same method cannot be used for arrays. Or am I wrong about that?

I've tried looping through the array, which works, but seems clumsy (and probably also computationally inefficient). 

    {% for number in request_contact.list_memberships %}
    
        {% if number == theSpecialNumber %}


Does anybody have a better method?


 



 

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Regular Advisor

I was playing around with some things and got this to work. using pprint

 

{% set list = request_contact.list_memberships|pprint %}
{% if number in list %}
Yes
{%else%}
No
{%endif%}

 

7 Replies 7
Regular Contributor

If variables could be set inside a for loop, I would just do this:

    {% for number in request_contact.list_memberships %}
    
        {% if number == list_number %}
        
            {% set hide_content = 'true' %}
        
        {% endif %}

    {% endfor %}

    {% if hide_content == 'true' %}
        Don't show irrelevant content.
    {% elif %}
        Show content to users who have not signed up.
    {% endif %}

 

But unfortunately this is not possible:

"Any variables defined within loops are limited to the scope of that loop and cannot be called from outside of the loop." – http://designers.hubspot.com/docs/hubl/for-loops

 

Esteemed Advisor

Have you tried using the request_contact.list_memberships as a variable?

 

{% set list = request_contact.list_memberships %}

{{ list }}

Then you could use most of the same code you have minus the for loop:

{% if number in list %}
        
     {% set hide_content = 'true' %}
        
{% endif %}

and

{% if hide_content == 'true' %}
        Don't show irrelevant content.
{% elif %}
        Show content to users who have not signed up.
{% endif %}

A version of this should work. Try outputting the array as a variable on page first, and if you can get the data to output you shouild be able to come up with the logic from there. I dont have any experience dealing with the membership or user variables though so I am only speculating. 

- Jonathan Sumner
Regular Advisor

 

If you are using pro or enterprise, smart content is an easier way to do what your trying to do. You can setup smart content at the template level or when creating a landing page.

https://knowledge.hubspot.com/smart-content-user-guide/how-to-get-started-with-smart-content

 

However, here's a solution to your problem.

 

You can use the "in" operator with an if statment to check whether a list contains the value. Just drop the for loop. Try this. 

{% if number in request_contact.list_memberships %}
{% set hide_content = 'true' %}
 {% endif %}

{% if hide_content == 'true' %}
        Don't show irrelevant content.
    {% elif %}
        Show content to users who have not signed up.
    {% endif %}

 

 

 

Reply
0 Upvotes
Regular Advisor

Sorry @Jsum! I just noticed that you posted the same solution yesteday.

Reply
0 Upvotes
Regular Contributor

@ndwilliams3 wrote:

 

If you are using pro or enterprise, smart content is an easier way to do what your trying to do. You can setup smart content at the template level or when creating a landing page.

https://knowledge.hubspot.com/smart-content-user-guide/how-to-get-started-with-smart-content

 

 

 

 


Smart content is unfortunately not available for custom modules (wonder why?)

 

At first I thought using "in" would be the right approach, like you and Jsum mentioned:

{% if number in request_contact.list_memberships %}

... but for some reason, this does not work. I've verified in the developer console that the number exists in the list_memberships array, and I've also printed out the array on the page to be sure. But the statement above validates to false, unfortunately.

I'd love to drop the loop – am I doing something wrong here? Full code:

{% set num_to_find = 65 %} 
(I've verified that 65 is in the array.)

{% if num_to_find in request_contact.list_memberships %}
  // The number is in the list membership array.
{% else %}
  // Number not found in array.
{% endif %}

 

 

 

Reply
0 Upvotes
Regular Advisor

I was playing around with some things and got this to work. using pprint

 

{% set list = request_contact.list_memberships|pprint %}
{% if number in list %}
Yes
{%else%}
No
{%endif%}

 

Regular Contributor

It looks like Hubspot has made some changes (or fixed) the way that "{% if variable in array %}" works, so now you apparently don't have to use "pprint" anymore.

This should work:

{% if number in list %}
Yes
{% else %}
No
{% endif %} 

 

Reply
0 Upvotes