Blog Post Tag Name If Statement - Correct Hubl Syntax

Trying to figure out what’s wrong with the following Hubl code-

{% for content in contents %}
...
{% if content.tag_list %}
 {% for tag in content.tag_list %}
 {% if tag.name == "Podcast" %}
 Html code goes here
 {% else %}
 Html code goes here
 {% endif %}
 {% endfor %}
{% endif %}
...
{% endfor %}

I can’t get the tag.name if statement to work. I’m sure it’s a simple syntax issue.

Thanks,

Terry McMillan

Hi @tmcmillan99 ,

Could you try the following code? I have this working in multiple projects:

{% for topic in content.topic_list %}
 {% if topic.name == 'Podcast' %}
 {% else %}
 {% endif %}
{% endfor %}

Also, you could try double checking the value of topic.name by using:

{% for topic in content.topic_list %}
 {{topic.name|pprint}}
{% endfor %}

Maybe the value isn’t exactly ‘Podcast’ and that’s why your code isn’t working.

@Teun Thanks for the suggestion. Would the syntax change if there was more than one tag/topic? I tested the topic.name|pprint and it prints the 2 topics assigned to the blog post with one being “Podcast”. However, the code displays both html code snippets instead of just one based on the if statement.

Thanks,

Terry

Hi @tmcmillan99 ,

Because we are looping through all the topics, both snippets will be displayed if a post has multiple topics.

If your goal is to display an HTML snippet whenever a post has the topic ‘Podcast’, regardless of the other topics, the code does change:

{% if content.topic_list|map('name') is containing "Podcast" %}
{% else %}
{% endif %}

Could you try the above code? I haven’t tested it. You should be able to skip the whole for loop where you are looping through the topic_list.

@Teun

I feel like the new map code should be working. However, it appears that it doesn’t like it for some reason as it’s displaying the html from the “else”.

If the “if” statment is true for Podcast, it should display a “Listen Now” link “else” it will display a “Learn More” link. The “Learn More” link is being displayed.

If I do content.topic_list|map(‘name’)|pprint, Podcast is part of the ArrayList that’s printed to the page - (ArrayList: [Podcast, Healthy Relationships Series])

Thanks,

Terry

@Teun

After testing numerous options, I was finally able to get it working correctly using the following code-

{% set topic_map = content.topic_list|map('name') %}
{% if topic_map is containing "Podcast" %}
html code
{% else %}
html code
{% endif %}

Thanks for the assistance and getting me pointed in the right direction.

Terry

Awesome! Happy you got it working!