Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Created September 25, 2022 00:19
Show Gist options
  • Save NickDeckerDevs/a94d9bd5a7dafd6a692ef02343db2f9b to your computer and use it in GitHub Desktop.
Save NickDeckerDevs/a94d9bd5a7dafd6a692ef02343db2f9b to your computer and use it in GitHub Desktop.
hubl for blog tag and hubdb comparison -- using a made up object to represent the hubdb table
{% set tags_from_blog_post = ['Inspiration','Teambuilding'] %}
{# this set ebooks would actually be from your HUBDB call #}
{% set ebooks = [
{
"name": "fred",
"tags": "smile, Brains",
},
{
"name": "correct",
"tags": "Inspiration, Brains",
},
{
"name": "both",
"tags": "Inspiration, Teambuilding",
}
]
%}
{# this is where we are going to store matches %}
{% set matches = [] %}
{% for ebook in ebooks %}
{# get the tags variable and split them by comma to create an array, using map('lower') to give us all lowercase
becauase it's better to match that way #}
{% set ebook_tags = ebook.tags|split(',')|map('lower') %}
{# using INTERSECT to compare the arrays: https://developers.hubspot.com/docs/cms/hubl/filters#intersect and using map #}
{# this will give us the tag names that match #}
{% set match = ebook_tags|intersect(tags_from_blog_post|map('lower')) %}
{# so you can see the output /debug %}
<h2>match: {{ match }}<br>tags: {{ ebook_tags|tojson }} </h2>
{# only use one of these below or you will just end up with all matches #}
{# |count gets you the total in the array #}
{# this is how we add and an item to the array, in your case we are adding the hubdb row to the
matches array so you can print that stuff out elsewhere #}
{# do ARRAYNAME.append(ITEM) #}
{# if you want to match on all values in the blog post #}
{% if match|count == tags_from_blog_post|count %}
{% do matches.append(ebook) %}
{% endif %}
{# if you want to get all the matches #}
{% if match|count > 0 %}
{% do matches.append(ebook) %}
{% endif %}
{# if you want to get the first match and thats it #}
{% if match|count > 0 && matches|count < 1 %}
{% do matches.append(ebook) %}
{% endif %}
{% endfor %}
<h1>{{ matches|tojson }}</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment