Conditional Statement Confustication

I’m trying to display different content, depending on how many posts a site contains. I can get the total number of pages with {{len .Site.Pages }}

However, if I try to build a conditional based on that value:

{{if lt 5  (len .Site.Pages)}}
///do stuff
//do other stuff

then the “other stuff” always gets displayed, no matter how many pages there are.

Answers on a postcard, to the usual address please!

  • You’re aware that lt is less than?
  • You’re on Hugo >= 0.15?

Works fine for me.

5 {{ if lt 5 (len .Site.Pages) }}<{{ else }}>{{ end }} {{ len .Site.Pages }}

Did you intend to use gt or ge?

Sorry. My bad. I should have said: “then the “other stuff” always gets displayed, no matter how FEW pages there are”

Seems like this should be working then, so I’ll have another look at it when I get back to the project. Just wanted to check I’d not cocked up the syntax, before beginning to apply forehead to wall.

Yep. Hugo Static Site Generator v0.15 BuildDate: 2015-11-25T14:34:54Z

Ah! –I’ve just realised where I was going wrong. Now I see why you two were asking if I was sure I meant lt rather than gt.

Misunderstanding of the syntax.

I was reading {{if lt 5 (len .Site.Pages)}} as meaning If (len .Site.Pages) is less than 5 but, of course it’s if 5 is less than (len .Site.Pages) –hence why I was getting the opposite result to what I wanted. Looks like whoever designed Golang’s template syntax was a fan of Polish Notation

[If anyone needs me, I’ll be over here in the corner, keeping this dunce hat warm!]


@stiobhart I think the correction intepretation would be to take the lesser of the two values rather than m seeking to see if site pages length is less than 5. I’m kind of asking because I’m confused about this conditional logic as well…

Have a look at that link to Polish Notation I put in my previous post. This template syntax kind of makes sense if you’re aware of the existence of that kind of mathematical notation.

If you’re not [or are expecting something a bit more like what they beat into us during maths lessons in school] it does take some getting your head around.

[Well, it does for me, anyway!]

It may help you if you think about these as functions with arguments, which is what they are:

func Lt(a, b) bool

Is a less than b?