If no results are found from range

Hello,

I have a simple range :
{{ range where .Site.RegularPages "Section" "evenements" }}

What I want to do is “if there are not results, do this”

{{ range where .Site.RegularPages "Section" "evenements" }}
{{ if results > 0}}
  {{ .Title}}
  {{ .Content}}
  {{ .Permalink}}
{{else}}
  no events yet, come back soon

You could check the length of list with evetns:

{{ $events := where .Site.RegularPages "Section" "evenements" }}
{{ if len $events | gt 1 }}
    // do the ranging
{{ else }}
    no events yet, come back soon
{{ end }}

Thank you! but where can I find this information in the doc? I could not find anything like this

What information exactly?

$events :=
is that variable declaration in GO?

if len $events | gt 1
if len…why len and not length, what is the | gt…all these things I cannot find in hugo documentation

Like where should I seek the info I need?

By default Hugo is using Go’s template engine. In the docs you can find a guide that explains the basics of it.

Go’s html/template package is an extensions of the text/html package which provides a small set of common template functions. You don’t have to understand Go, it’s just a background info.

Hugo extends the set of common template functions to fill the toolbelt for a static site generator. Here is a list of those functions.

Exactly. It doesn’t seem to be hard

Well, the text/html package named this function len. Here’s an overview all functions provided by text/template. As I said Hugo just adds a bunch of other useful template functions that you can find here.

The | operator is similar to the Unix pipe. It basically pipes the output of the previous function as input to the next function. This allows you to chain multiple operations elegantly. However, I would refer again to the Go template primer.

Btw, @rdwatters started to rewrite the documentation. Please let us know were exactly you got stuck and what can be improved. We try to incorporate the feedback from new users.

Thank you so much for the detailed answer, I very much appreciate it.

Oddly I had to put my range in the else, not in the if…

Well I got stuck that I had no idea I could do this : {{ $events := where .Site.RegularPages “Section” “evenements” }}

basically store a where in a variable. When I print that variable {{ printf “%#v” $events }} I get :
hugolib.Pages{(*hugolib.Page)(0xc050892f00)}

Don’t really know what to do with that and if I just add {{ $events }} I get :
Pages(1)

Not sure what to do with that either. I’m use to the javascript world where you would do something like {{ $events.length }}

something like that.

Also there seems to be two docs :
https://hugodocs.info/templates/introduction/#logic

Kinda confusing

So that explains how I got stuck, hopefully this info helps improve the docs

When I removed the | gt 1

It worked as expected. Maybe because the counting starts at 0 or something like that?

where gives you a list of all pages (which are somewhat similar to objects in JS). Such a list can be assigned to a variable as you already know it. where also allows even some more fine grained filtering for which you can find examples in the docs.

This way you print the internal representation of the pages list.

I’m not quite sure but I guess you only have a single event. Nonetheless, since you have a list assigned to $events you can range over it like in JS with range.

Pages aren’t really objects in the way that you can’t call methods with the dot notation. Instead you pass the “object” as an argument to a function, e.g. len $events.

https://gohugo.io are the official docs. https://hugodocs.info used to be the rewritten version by @rdwatters. I linked both because the latter one is basically a revamped version. Don’t be confused. I just linked both so that you get the best of both worlds.

Thank you so much for all the information, I’m really started to “get” how hugo works.

Will Hugo use @rdwatters doc as official eventually?

Thanks!

2 Likes

By the way this forum has been life saving on so many different occasion! Hopefully the docs will improve and I won’t come here as often!

1 Like

Yes. We’re also planning a redesign of the website made by @budparr. Once the design is done we’ll merge the content and put them on www.gohugo.io. Stay tuned!

Sometimes it takes a bit of time and some pointers to grasp new tools.

1 Like

Very exciting stuff! Thanks!

You guys are killing it on the support forums…for something that is free, it’s incredible to be honest. I get answers in less then 10 minutes usually

Thanks. I’m just watching the forum while doing other stuff. The Hugo community is rather small but we try our best to help. A 10 min deadline is hard to keep, so don’t set your expectations too high :smile:

1 Like

No exactly, every time I post I’m like okay probably 24-48h. It just so happens that every time I post, it’s super fast haha

Anyway thank you so much for everything, it has been “life saving”