.Site.RegularPages always returns none in archetype

Hi, I’m trying to modify the archetype for one of my projects so that each new page that is created is automatically given a weight equal to the number of other pages in its section, essentially making each new page default to the end of the list.

To accomplish this, I added the following code to my archetype.
weight: {{len (where .Site.RegularPages "Section" .Section)}}

However, it seems when my archetype gets processed, .Site.RegularPages always returns no pages. This entry in the docs says “The full Hugo .Site and all of Hugo’s template funcs can be used in the archetype file.”

That doc even gives an example of including
{{ range first 10 ( where .Site.RegularPages "Type" "cool" ) }}
* {{ .Title }}
{{ end }}
in your archetype, but even when replacing where .Site.RegularPages "Type" "cool" with just .Site.RegularPages that function fails to return the titles of any of the pages in my site.

I included the following code in my archetype to test what was happening.
{{.Site.Pages}}
{{.Site.RegularPages}}
Which, when a new page was created, returned:
Pages(3)
Pages(0)

Just to double check, I included the same {{.Site.RegularPages}} snippet in one of my partials, and that returned the correct Pages(#), so I do believe I have regular pages in my project.

Any ideas as to what is going wrong here?

Thanks,
Will

archetypes/default.md

---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
weight: {{ len (where .Site.RegularPages "Section" .Section) }}
---

Create 3 new pages in a new section:

hugo new foo/test-0.md
hugo new foo/test-1.md
hugo new foo/test-2.md

All will have weight zero.

Now, change draft to false in any two of the files, then create a new page:

hugo new foo/test-3.md

The weight of test-3.md will be 2.

So, in this context, the .Site.RegularPages collection only contains non-draft items.

You can work around this by setting draft to false in your archetype.

1 Like

Thanks for your help jmooring. I setup a totally clean project and verified that in fact, this does work there. I took a series of steps to try and figure out why it works in a new project but not in my existing one, and found that the point it stops working at is once there is a file named ‘index.md’ in the content directory.

I created an example repo with your setup, except with an additional empty index.md file, so you can hopefully replicate the issue I’m seeing. https://github.com/willbicks/hugo-weight-test

Is it possible I’m missing something? My projects has a non-list homepage, so that’s why it uses an ‘index.md’ in the root of the content directory, which from my interpretation of the HUGO docs is legitimate.

Read this:
https://discourse.gohugo.io/t/content-depth-migrating-from-jekyll/26635/5

You don’t want an index.md in content/.

Got it! My understanding was that since my index.md wasn’t listing my other pages I didn’t want to describe it as a list but thanks for sharing that information. By renaming it _index.md I got the expected result!

Thanks.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.