Range newbie: how to control range with respect to subfolders

Hi. Thanks all for the Hugo experience. Currently migrating my existence away from Word Press.

I’ve been gradually getting under the hood of Hugo, into the templates. It’s not 100% comprehensible

I’m finding the range command to be a particularly confusing aspect of Hugo. I’ve searched quite a bit but haven’t been able to master it yet. (I’ll search some more here in the forum, in case I’ve missed anything.)

I’m using the Mainroad theme.

What I’ve got are long, complex documents which I’ve broken up into pieces, like this:


Each part has a “triangle = n” in the frontmatter, if that helps. So part3.md has ‘triangle: “3”’ It also contains a “geo = n” so that in this example part2.md would contain "geo = “10”. I wanted to use this information to help me sort out which pages to show. But I have not been able to figure it out.

Right now, everything in geometry, including all the triangle posts are showing up on the homepage. What I want is for only the main geometry posts to show up, not the contents of the triangle folder, or any folder subordinate to the folders listed in the “mainSections” in the config file.

When users click on the "geometry’ menu button, I want to get a list of all the geometry posts, with the triangle posts as an ordered list beneath, just the titles. I actually managed to do this in the list template to some extent. But I was unsatisfied with my approach, which used an if statement in the summary.html template (called by the list template) to check if the post was a part or not, then treating it differently (not including a summary or thumbnail or metadata). The proper way to do this is to check for parts in triangle for each main geo-post. But I couldn’t figure out (a) how to use range in the list template to restrict it from including the unwanted triangle posts, and (b) how to use range in the summary.html template to range over the triangle folder for each geo post.

I’m sorry I don’t have any code, because frankly nothing I have written makes any sense, and I’m back at square one basically with mainroad. Here is the relevant mainroad code in the list template:

		{{- range .Paginator.Pages.ByPublishDate.Reverse }} 
			{{- if eq .Params.triangle "" }}
				{{- .Render "summary" }}
			{{- else }}
			{{- end}}

I’ve changed this code to “.ByPublishDate.Reverse” for my own purposes. Right now it posts a summary if the triangle param has not been set. That means it’s not a post in /triangle/. But this is inelegant because as far as Hugo is concerned, it still counts the unprinted triangle post as a post for paginate which is set to a constant. If a geo has 5 triangle parts, it eats up half the pagination. So we want a better range command that only gives us what we want, then “go into” that page, and grab the triangle pages relevant to that geo.

Mainroad calls summary.html default template, which inserts a thumbnail, title, metadata, and a summary. I tried to insert a range command in summary.html. But when I did I just get entangled and confused. Plus, I know the for the list template is already going to call these triangle parts. It’s a big mess.

My head is just spinning with templates calling templates with ranges inside ranges. I don’t know what context I am in, .Site .Pages? And then there’s the .Paginator which on the one hand people use to get fancy page navigation bars, but also seems to be an essential part of the list template. When I look for examples I keep finding really different answers and none of them add up to something I can understand.

Just to remind you:

  1. On the one hand, for the homepage I want to ignore the triangles subfolder.

  2. On the other hand, I want to grab the posts from the triangles subfolder and insert the titles.

The question is, how do we include or exclude sub-folders in the range command? Another question, how can we start a new range command from within a sub-template?

Or is there a Hugo range command introduction somewhere that I can read to master this command? (The Hugo docs are helpful but not always helpful enough.)


Hello third_try,

Please provide a link to the source code repository for your project.
See https://discourse.gohugo.io/t/requesting-help/9132.

I don’t think this is exactly what you’re asking for, but it might lead you in the right direction.

Wow. What a gift. I’m going to study this.

I’m sorry I didn’t have a proper bit of code to show. Like I said, I was very confused and hitting dead ends everywhere. But I’m sure you’ve given me enough to figure out and learn from. If I need any follow up help, I’ll be sure to show more code.

Thanks very much.

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