We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
Aug 7, 2018 5:22 AM
Hi!
I'm creating a template for blog posts listing.
One of the topics is for service use only ("Highlight"), I use it to show in particular listing.
I need to show the first topic for each post but not "Highlight".
So I need to remove this topic from topic list of single post.
I have tried to filter topics with "rejectAttr":
// getting topics from post:
{% for topic in content.topic_list|rejectattr('name', 'string_startingwith "High"')%}
{% if loop.index <= 1 %}
<div class="topic">{{ topic.name }}</div>
{% endif %}
{% endfor %}
But it doesn't work at all: I get an empty array after filtering.
Be so kind to tell me how to use rejectattr in this case?
Or is there another way to filter out the topic that I don't want to be in my listing?
Thank you in advance!
Solved! Go to Solution.
Aug 7, 2018 9:20 AM - edited Aug 7, 2018 9:37 AM
@Octo - I see... I don't think you can chain an expression test to a filter in a loop since the loop hasn't returned the string yet and you can't access the dict child of name in the topic. I think this might be a better approach (maybe less elegant though)
{% for topic in content.topic_list %} {% if !topic.name is string_startingwith "High" %} <div class="topic">{{ topic|pprint }}</div> {% endif %} {% endfor %}
Or, slightly more elegant...
{% for topic in content.topic_list %}
{{ '<div class="topic">'+topic.name+'</div>' if !topic.name is string_startingwith "High" }}
{% endfor %}
If this answer helped, please, mark as solved 😄
tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.
Drop by and say Hi to me on slack.
Aug 7, 2018 8:05 AM
Hello @Octo - I'm not seeing the string_startswith filter in the HUBL documentation. Can you confirm that it exists in there with a link?
I think you will have better results if you just do a straight string comparison on the rejectattr filter
{% for topic in content.topic_list|rejectattr('name', 'Highlight')%}
If this answer helped, please, mark as solved 😄
tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.
Drop by and say Hi to me on slack.
Aug 7, 2018 9:01 AM
Hi @tjoyce!
Thank you for your reply, but it doesn't works for me 😞
I got an empty sequence.
> I'm not seeing the string_startswith filter in the HUBL documentation
Second argument for rejectattr is expression test, so I found string_startswith in expression tests list:
https://designers.hubspot.com/docs/hubl/operators-and-expression-tests
Aug 7, 2018 9:20 AM - edited Aug 7, 2018 9:37 AM
@Octo - I see... I don't think you can chain an expression test to a filter in a loop since the loop hasn't returned the string yet and you can't access the dict child of name in the topic. I think this might be a better approach (maybe less elegant though)
{% for topic in content.topic_list %} {% if !topic.name is string_startingwith "High" %} <div class="topic">{{ topic|pprint }}</div> {% endif %} {% endfor %}
Or, slightly more elegant...
{% for topic in content.topic_list %}
{{ '<div class="topic">'+topic.name+'</div>' if !topic.name is string_startingwith "High" }}
{% endfor %}
If this answer helped, please, mark as solved 😄
tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.
Drop by and say Hi to me on slack.
Aug 7, 2018 9:43 AM
Thanks for the help!
This will work, but I'll have to hide all the following topics except the first. Also, this will increase the HTML a bit.
I'll wait for a while. maybe someone will help with a solution to filter the data.
Aug 7, 2018 9:53 AM
@Octo - The html should actually be smaller if you use the second option I gave you. Also, not quite sure what you mean by the HTML being smaller. HUBL code is ran on the server side and not the client side.
If you're worrying about too much data being pulled into the template, you shouldn't have to worry about that since all the filter does is sanitize the data after it's available anyway. You can see this by viewing the developer info for the page.