CMS Development

vin-aws
投稿者

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

解決

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 いいね!
1件の承認済みベストアンサー
MFrankJohnson
解決策
ソートリーダー

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

解決

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

元の投稿で解決策を見る

5件の返信
MFrankJohnson
解決策
ソートリーダー

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

解決

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
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

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

解決

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

0 いいね!
vin-aws
投稿者

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

解決

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

0 いいね!
MFrankJohnson
ソートリーダー

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

解決

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
投稿者

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

解決

Yeah the parenthesis worked for some reason...