This works in header.html to determine if I’m on the home page:
{{ if .IsHome }}
<div class="container-fluid">
{{ else }}
<div class="container">
{{ end }}
But this does not work in header.html to determine if I’m either on the Home page or on the page merch.html in /pages/merch.html:
{{ if ne .IsHome | or (ne .Permalink "/pages/merch") }}
<div class="container-fluid">
{{ else }}
<div class="container">
{{ end }}
Is my use of .Permalink wrong? Should I use GetPage?
I think that you might want to try
if (or (ne .IsHome) (ne .Permalink "/pages/merch"))
All the operators are prefix, not infix. Also, what do you intend to achieve with the |
sign in your code?
Thanks, but the conditionals in
if (or (ne .IsHome) (ne .Permalink "/pages/merch"))
don’t work.
In pseudo code, what I want to do is
{{ If is not Home or if is not /merch }}
this
{{ else }}
this
{{ end }}
and for another area in the site,
{{ If is Home or if is /merch }}
this
{{ else }}
this
{{ end }}
The | sign was my mistake.
bluedogranch:
don’t work.
What does that mean? You get an error message or it doesn’t do what you expect?
It shows <div class="container-fluid">
on all pages, including home and merch.
eq
, ne
, lt
, le
, gt
, and ge
are comparison operators. They are for comparing one thing to another thing.
ne .Permalink "/pages/merch" --> compares two things
ne .IsHome --> does not compare two things
To invert a boolean (true/false) value, use not
.
Thanks, that makes sense. How would I use Not in the pseudocode
{{ If is not Home or if is not /merch }}
{{ if or (not .IsHome) (ne .RelPermalink "/pages/merch/") }}
3 Likes
Thanks, that’s interesting; I see the or construction now. This works in header.html:
{{ if or (.IsHome) (eq .RelPermalink "/pages/merch/") }}
<div class="container-fluid">
{{ else }}
<div class="container">
{{ end }}
But this for some reason this doesn’t work in baseof.html and still shows the sidebar on merch and home; why does ne
not work as not equal? Or what else should I use?
{{ if or (not .IsHome) (ne .RelPermalink "/pages/merch/") }}
{{ partial "sidebar.html" . }}
{{ end }}
Please edit your post. Use code fences (three backticks) instead of blockquotes (>
).
You have a logic problem…
Current Page
not .IsHome
ne .RelPermalink "/pages/merch/"
or
returns
and
returns
/
false
true
true
false
/pages/merch/
true
false
true
false
/foo
true
true
true
true
If want to do a thing on every page, but exclude home and /pages/merch/ …
{{ if not (or .IsHome (eq .RelPermalink "/pages/merch/")) }}
do a thing
{{ end }}
This will do the same thing, but I personally don’t care for the double negation …
{{ if and (not .IsHome) (ne .RelPermalink "/pages/merch/") }}
do a thing
{{ end }}
4 Likes
Ah, I see in the first example that not
has to be outside the (or...)
construct.
Starting to make sense
Thanks.
system
Closed
February 5, 2022, 4:12am
13
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.