Help converting if else statement to or

So I’m trying to use a conditional or to verify if the current url equals one of 2 pages in order to display a partial, based on a few searches and reading I ended up with this:

{{if or (ne .URL "/search/") (ne .URL "/contact-us/")}}
{{partial "related-articles.html" .}}

I get no errors in the console but the partial still displays on both of these urls I ended up using a terrible if/else statement but would like to learn how to properly do it using the or conditional.

Any help is greatly appreciated.

ne is “not equals”. So what you have there says “if URL is not equal to /search/ or URL is not equal to /contact-us/”, which is always true (the only time it would fail is if URL was equal to both those values at the same time, which isn’t possible).

Try replacing ne with eq, which means “equal”.

So the or statement can’t check if it’s either or?

In javascript I can do:

if ( value === true || value === 1) {return value}

I can’t do the same with hugos or conditional?

Right now I’m doing this but it feels so dirty:

{{if eq .URL "/search/"}}
{{else if eq .URL "/contact-us/"}}
{{partial "related-articles.html" .}}

It works but it feels so wrong and I do want to learn how to use the or operator.

You don’t have to rely on else if and else. Here are a few equivalent statements that might look a bit cleaner:

{{ if ne .URL "/search/" | and (ne .URL "/contact-us/") }}

{{ if not (eq .URL "/search/" | or (eq .URL "/contact-us/")) }}

{{ if  .URL | in (slice "/search/"  "/contact-us/") | not }}

{{ if  not (.URL | in (slice "/search/"  "/contact-us/")) }}
1 Like

Sure, that’s what “or” does. Since you know javascript, that makes this a bit easier to explain… ne is the equivalent of !=, so what you have written above would be roughly akin to

if (value != "/search/" || value != "/contact-us/") {

What you need is eq, which is the equivalent of ==.

{{ if or (eq .URL "/search/") (eq .URL "/contact-us/)") }}
  {{ partial "related-articles.html" }}
{{ end }}

I want to hide it on those 2 pages and show it on every other page not show them on those pages, if that makes sense.

Thanks for replying both you and @digitalcraftsman I will try some of your suggestions and see if the provide the result I want, cheers.

Ended using this one works great thanks for these examples really helps understand how these work a bit better.