Checking for the presence of a value in an array (list_membership)
SOLVE
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 %}
Checking for the presence of a value in an array (list_membership)
SOLVE
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 %}
Checking for the presence of a value in an array (list_membership)
SOLVE
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.
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 %}
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.
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 %}
Checking for the presence of a value in an array (list_membership)
SOLVE
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 %}
Checking for the presence of a value in an array (list_membership)
SOLVE
If you want to more then one value here's an update.
My situation was a very common one.
Check if visitor is in one (or more) list(s).
so this:
{% 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 %}
becomes this:
{% set obj = {val : 0} %} {% set IDs_to_find = module.list_id_for_smart_resource|split(',') %}
// the content editor via a text field can add one or more comma separated ids (module.list_id_for_smart_resource)
// looping trough the IDs and incrementing the obj if any is found {% for id in IDs_to_find %}
{% if id in request_contact.list_memberships %}
{% do obj.update({val: obj.val + 1}) %} {% endif %}
{% endfor %}
// now outside the loop we can check if obj.val has increased
{% if obj.val > 0 %} YES content {% else %} NO content {% endif %}
Checking for the presence of a value in an array (list_membership)
SOLVE
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.