CMS Development

Octo
メンバー | Platinum Partner
メンバー | Platinum Partner

Excluding a blog topic from a Topics list

解決

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!

0 いいね!
1件の承認済みベストアンサー
tjoyce
解決策
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Excluding a blog topic from a Topics list

解決

@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.

元の投稿で解決策を見る

0 いいね!
5件の返信
tjoyce
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Excluding a blog topic from a Topics list

解決

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.

0 いいね!
Octo
メンバー | Platinum Partner
メンバー | Platinum Partner

Excluding a blog topic from a Topics list

解決

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

0 いいね!
tjoyce
解決策
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Excluding a blog topic from a Topics list

解決

@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.

0 いいね!
Octo
メンバー | Platinum Partner
メンバー | Platinum Partner

Excluding a blog topic from a Topics list

解決

@tjoyce

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.

 

0 いいね!
tjoyce
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Excluding a blog topic from a Topics list

解決

@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. 

0 いいね!