CMS Development

vin-aws
Contributor

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

SOLVE

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
MFrankJohnson
Solution
Thought Leader

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

SOLVE

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: Please search for recent posts as HubSpot evolves to be the #1 CRM platform of choice world-wide.

 

Hope that helps.

 

Be well,
Frank


www.mfrankjohnson.com

View solution in original post

5 Replies 5
MFrankJohnson
Solution
Thought Leader

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

SOLVE

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: Please search for recent posts as HubSpot evolves to be the #1 CRM platform of choice world-wide.

 

Hope that helps.

 

Be well,
Frank


www.mfrankjohnson.com
tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

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

SOLVE

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

0 Upvotes
vin-aws
Contributor

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

SOLVE

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

0 Upvotes
MFrankJohnson
Thought Leader

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

SOLVE

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: Please search for recent posts as HubSpot evolves to be the #1 CRM platform of choice world-wide.

 

Hope that helps.

 

Be well,
Frank


www.mfrankjohnson.com
vin-aws
Contributor

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

SOLVE

Yeah the parenthesis worked for some reason...