CMS Development

malcolm_
Participante

"If A or B" statement syntax?

I'd like to have an element display on the page IF the URL of that page contains any of a handful of strings. I'd like to have this IF statement be one line (IF a OR b OR c), but I'm having trouble trying to get it to work.

 

The Operators & Expression Tests page mentions "OR" with the description, "Return true if the left or the right operand is true" but doesn't use it in any examples. The IF Statements page only shows "IF this then this, ELIF this then this" which results in duplicate blocks of code for every string.

 

For example, I would like to display the publish date of the blog post IF the URL does not contain "/home", "/voip-fundamentals" or "/voip-solutions". Right now, I'm accomplishing this with the following:

 

  {% unless content.absolute_url is string_containing '/home' %}
  {% unless content.absolute_url is string_containing '/voip-resources/voip-fundamentals' %}
  {% unless content.absolute_url is string_containing '/voip-resources/voip-solutions' %}
        <em>Published: {{ content.publish_date|datetimeformat('%B %e, %Y') }}</em>
  {% endunless %}
  {% endunless %}
  {% endunless %}

This isn't very pretty or scalable. I've tried the following with no success:

 

  {% unless content.absolute_url is string_containing '/home' or content.absolute_url is string_containing '/voip-resources/voip-fundamentals' or content.absolute_url is string_containing '/voip-resources/voip-solutions' %}
  <em>Published: {{ content.publish_date|datetimeformat('%B %e, %Y') }}</em>
  {% endunless %}
  {% unless content.absolute_url is string_containing '/home' or '/voip-resources/voip-fundamentals' or '/voip-resources/voip-solutions' %}
  <em>Published: {{ content.publish_date|datetimeformat('%B %e, %Y') }}</em>
  {% endunless %}

Is it possible to do this with HubL?

0 Avaliação positiva
1 Resposta 1
lscanlan
Alunos da HubSpot
Alunos da HubSpot

"If A or B" statement syntax?

Hi @malcolm_,

 

I'm not sure if you ever found a solution to this, but I think I see what the issue is here. Just for reference, you're writing your expression test like:

 

{% unless content.absolute_url is string_containing '/home' or content.absolute_url is string_containing '/voip-resources/voip-fundamentals' or content.absolute_url is string_containing '/voip-resources/voip-solutions' %}

Let's look at how those expressions are being evaluated. The first part: unless content.absolute_url is string_containing '/home' is pretty straightforward. Basically: unless content.absolute_url contains the string 'home', this evaluates to true. But the right side of that OR operator is actually just checking to see if content.absolute_url contains the string '/voip-resources/voip-fundamentals'. So if it does, it evaluates to true, because the unless keyword isn't being used here. Which is actually the opposite of what you want. You want it to evaluate to true only when it doesn't contain that string. So that's why your expression test wasn't working the way you were expecting it to.

 

I think you can re-write it with parentheses to make it work how you're expecting. If you write it like this:

 

{% unless (content.absolute_url is string_containing '/home') or (content.absolute_url is string_containing '/voip-resources/voip-fundamentals') or (content.absolute_url is string_containing '/voip-resources/voip-solutions') %}

...then it will run the expression test against each of those expressions and the OR operator will work how you're expecting. This will evaluate to true when content.absolute_url doesn't contain any of '/home', '/voip-resources/voip-fundamentals', '/voip-resources/voip-solutions' .

 

Let me know if that makes sense and does the trick for you.

 

Leland Scanlan

HubSpot Developer Support