Skip to main content
Solved

Using IF conditional with a numeric range


doug s
Contributor II
Forum|alt.badge.img+2

What I’m trying to test is if the length of a list is between two numbers.

What I’ve tried so far:

  • {% elif event|lookup:'Item Count' > 1 &&  < 4 %}
  • {% elif event|lookup:'Item Count' > 1 && event|lookup:'Item Count' < 4 %}

I could keep on going like this, just randomly trying anything I can think of, but thought I’d come ask the experts.

Is what I’m trying to do possible?

(NOTE: I have a properly formatted ‘if’ condition, opened and closed correctly, etc. It worked fine until I tried to do the ‘greater than this but less than this’ bit.)

Best answer by saulblum

The first one — {% elif event|lookup:'Item Count' > 1 &&  < 4 %} — won’t work, as each individual condition needs to be fully formed, i.e. you need to repeat event|lookup:'Item Count'.

The second one should work. We’ve seen issues recently where < and > were getting encoded as HTML entities &lt; and %gt;

Is that happening here? If you save the block and go back to edit it, do you see the < and > now escaped?

If so, we do have django filters gt and lt you can try:

gt

Use this filter to return the value of the expression "item is greater than value" (item > value) where item is passed first and value is passed second.

{% if 3|gt:2 %}

<p>3 is greater than 2</p>

{% endif %}

 

{% if 2|gt:3 %}

<p>2 is greater than 3</p>

{% endif%}



 

AD_4nXeN8XIilenjzwWmfjtu2fkWK7XeG7b-4Nblz7GWFC4xa2l9oqTrPMhlgG0fIZU8PuS41GfwTUnFZsyzLbPh2hEb9Ba6bbutIbuLiGBvR-lW-b3dSxmcEAx-PP6X0qm5UnKdBoNlKxb9mMqf7Q2pb2zy5HU?key=OYxZ8WLD13gJj67YrSpTDA

 

lt

Use this filter to return the value of the expression "item is less than value" (item < value) where item is passed first and value is passed second.

{% if 2|lt:3 %}

<p>2 is less than 3</p>

{% endif %}

 

{% if 3|lt:2 %}

<p>3 is less than 2</p>

{% endif %}

AD_4nXc7MiePsAaPrMER9rtuxAmED61aWIEEi3CxBzjIdnjyn3GDdTcadTPzlwOBmdNDnSvJ1HeOWl0IPbxplO43NuSaEG3MiTRClF1KhprCDcE_pG11KLnCmrflw6rFXEbuS8SXbmCN3pjY0wVOUYRMmMGndg?key=OYxZ8WLD13gJj67YrSpTDA

 

View original
Did this topic or the replies in the thread help you find an answer to your question?

3 replies

Forum|alt.badge.img+7
  • Klaviyo Employee
  • 169 replies
  • Answer
  • July 29, 2024

The first one — {% elif event|lookup:'Item Count' > 1 &&  < 4 %} — won’t work, as each individual condition needs to be fully formed, i.e. you need to repeat event|lookup:'Item Count'.

The second one should work. We’ve seen issues recently where < and > were getting encoded as HTML entities &lt; and %gt;

Is that happening here? If you save the block and go back to edit it, do you see the < and > now escaped?

If so, we do have django filters gt and lt you can try:

gt

Use this filter to return the value of the expression "item is greater than value" (item > value) where item is passed first and value is passed second.

{% if 3|gt:2 %}

<p>3 is greater than 2</p>

{% endif %}

 

{% if 2|gt:3 %}

<p>2 is greater than 3</p>

{% endif%}



 

AD_4nXeN8XIilenjzwWmfjtu2fkWK7XeG7b-4Nblz7GWFC4xa2l9oqTrPMhlgG0fIZU8PuS41GfwTUnFZsyzLbPh2hEb9Ba6bbutIbuLiGBvR-lW-b3dSxmcEAx-PP6X0qm5UnKdBoNlKxb9mMqf7Q2pb2zy5HU?key=OYxZ8WLD13gJj67YrSpTDA

 

lt

Use this filter to return the value of the expression "item is less than value" (item < value) where item is passed first and value is passed second.

{% if 2|lt:3 %}

<p>2 is less than 3</p>

{% endif %}

 

{% if 3|lt:2 %}

<p>3 is less than 2</p>

{% endif %}

AD_4nXc7MiePsAaPrMER9rtuxAmED61aWIEEi3CxBzjIdnjyn3GDdTcadTPzlwOBmdNDnSvJ1HeOWl0IPbxplO43NuSaEG3MiTRClF1KhprCDcE_pG11KLnCmrflw6rFXEbuS8SXbmCN3pjY0wVOUYRMmMGndg?key=OYxZ8WLD13gJj67YrSpTDA

 


doug s
Contributor II
Forum|alt.badge.img+2
  • Author
  • Contributor II
  • 4 replies
  • July 31, 2024
saulblum wrote:

… The second one should work. We’ve seen issues recently where < and > were getting encoded as HTML entities &lt; and %gt;

Is that happening here? If you save the block and go back to edit it, do you see the < and > now escaped? …

Not that I recall. It all looked clean, as I went back into the block several times to make tweaks and re-test. But… that doesn’t mean they couldn’t have been encoded.

And now, things have changed with that particular email, as it turns out we weren’t able to return the desired data that this section was written for in the first place, so it’s all moot now. However, my curiosity won’t let me rest on this, so I may create a test email to try this out on—I like things to be resolved. I’ll report back here with my findings.

Thanks for your response.


doug s
Contributor II
Forum|alt.badge.img+2
  • Author
  • Contributor II
  • 4 replies
  • July 31, 2024

The ‘greater than’ / ‘less than’ signs were not being encoded to HTML entities; it was the double ampersand causing the error. When I changed “&&” to “and”, everything worked as intended.

Thanks again for your input.