Range .Pages from single template

That said, I understand I might be doing this is a non standard way. Not sure what happened between the Hugo Quickstart and now. I’ll review my steps.

OK.

On my end the URL http://localhost:1313/posts/16-medical-benefits-marijuana/ threw a 404 locally.

I had to delete draft = "true" from the post’s front matter.

So here is why that page got a 404 on Netlify. Drafts are not published.

As for your missing loop:

First, you do not need the dollar sign ($). You would use that if you where within a shortcode to go higher up.

Second .Data.Pages is for list pages not single pages.

You should use .Site.Pages for a single page template to render other pages.

[ Not entirely unrelated note. Hey @kaushalmodi (thought you wanted to see this) here is a use case where simply using {{ range .Pages }} doesn’t return anything only .Site.Pages returns results ]

But anyway @jnthnclrk are you sure you want to display post permalinks in your header? You will need 2 different headers with such a design. One for single pages and one for lists. Not very DRY (Don’t Repeat Yourself)

Also see related topic about .Data.Pages vs .Site.Pages from a single page template:

1 Like

I don’t have the full context – {{ range .Pages }} would get pages only from the current… um… context, whereas {{ range .Site.Pages }} will get all pages from the site regardless of the current context.

So {{ range .Pages }} might not return all site pages if it’s placed in a narrower context.

1 Like

The current context is a single page.


P.S. I renamed this thread to something more appropriate since it had nothing to do with Netlify after all.

Yep, then that returning nil makes complete sense… A single page never contains a slice of other pages (talking strictly about .Pages here).


Update: See this table for more info.

1 Like

You should merge that table in the Docs.

@alexandros you nailed it! My original problems were two fold:

  1. My posts were draft - DOH!
  2. I was using the wrong function across single and list pages

Thing is, .Site.Pages works on both single and list pages. So instead of writing 2 header partials, I may as well keep that.

.Site.Pages will fetch all pages. Including static pages such as About or Contact that you may not want included.

In any case you should probably limit what is returned by your range function, there are plenty of examples in the Docs.