How to negate an {% if ... in ... %} statement

SOLVE
vin-aws
Contributor

I am trying to filter blog posts in a listing template and have

 

{% if "my tag" in content.topic_list|pprint %}
// HTML Markup
{% endif %}

Which shows posts when they are tagged with "my tag". How do I negate this to show posts that do NOT contain "my tag". I've tried inserting "not" everywhere in the statement with no success.

 

{% not if "my tag" in content.topic_list|pprint %}
{% if not "my tag" in content.topic_list|pprint %}
{% if "my tag" not in content.topic_list|pprint %}
{% if "my tag" in not content.topic_list|pprint %}

{% if "my tag" is not in content.topic_list|pprint %}

All don't work.

 

Also side note... why do I have to pprint to be able to use the "in" operator... It doesn't work if I just call it on content.topic_list. Seems like a bug to me.

0 Upvotes
1 Accepted solution

Accepted Solutions
MFrankJohnson
Solution
Thought Leader

This code isn't pretty, but should work ... 

{% if "my tag" in content.topic_list|pprint %}
{% else %}
  // HTML Markup
{% endif %}

This also works.  ( tested by @vin-aws )

{% if not ("my tag" in content.topic_list)|pprint %}
  // HTML Markup
{% endif %}

Couldn't find any examples of the logical 'not' operator documented for use with the 'in' operator. Maybe some of the other guys know @tjoyce@Stephanie-OG

 

Hope that helps a little.

 

Note: The date of each post here is increasingly important as HubSpot evolves to be the #1 CRM platform of choice world-wide.

-- Visit

 

Hope that helps.

 

Be well,
Frank


HubSpot's Hiring World-Wide!

View solution in original post

5 Replies 5
MFrankJohnson
Solution
Thought Leader

This code isn't pretty, but should work ... 

{% if "my tag" in content.topic_list|pprint %}
{% else %}
  // HTML Markup
{% endif %}

This also works.  ( tested by @vin-aws )

{% if not ("my tag" in content.topic_list)|pprint %}
  // HTML Markup
{% endif %}

Couldn't find any examples of the logical 'not' operator documented for use with the 'in' operator. Maybe some of the other guys know @tjoyce@Stephanie-OG

 

Hope that helps a little.

 

Note: The date of each post here is increasingly important as HubSpot evolves to be the #1 CRM platform of choice world-wide.

-- Visit

 

Hope that helps.

 

Be well,
Frank


HubSpot's Hiring World-Wide!

View solution in original post

vin-aws
Contributor

Oh yeah slipped my mind I could just use an 'else'

0 Upvotes
MFrankJohnson
Thought Leader

Yea ... but it ain't 'pretty'. haha And you know we like our code 'pretty'. lol 🙂

 

Will you please test out that 'not' alternative and let us know @vin-aws?

 

Logic doesn't always prevail when it comes to code syntax.

 

Note: The date of each post here is increasingly important as HubSpot evolves to be the #1 CRM platform of choice world-wide.

-- Visit

 

Hope that helps.

 

Be well,
Frank


HubSpot's Hiring World-Wide!

vin-aws
Contributor

Yeah the parenthesis worked for some reason...

tjoyce
Recognized Expert | Platinum Partner

@vin-aws@MFrankJohnson is right, this is the way to do it with the {% else %} statement

0 Upvotes