Custom page as the index of a section, and listing content anywhere

Viewed 201

I'm new to Hugo, so excuse if I use the wrong terms - please correct me if I do. Let me explain what I need to do.

Let's consider a travel agency's site. The agency has "offers", and they are organized into "categories" (and maybe subcategories, if the category has a parent property in the front matter). If I got Hugo right, "offers" and "categories" should be two sections, and I should have a folder structure like this, with each page in content/offers referencing a category in its front matter:

content
|- categories
|  |- europe.md
|  |- americas.md
|  |- asia.md
|  |- italy.md
|  |- france.md
|- offers
   |- a-week-in-paris.md
   |- new-york-and-the-east-cost.md
   |- milan-unknown-history.md
   |- indian-charm.md

I want to make a category's page so that it lists its subcategories and its offers: I should do this in the layouts/categories/single.html layout, am I right? However, how do I get the collection of its subcategories (i.e., categories that have "this category" in the parent property) or the collection of its offers (i.e., offers that have "this category" in the category property)?

I also want to have a "root" page for the offers, that only shows the list of the five top categories (i.e., categories that don't have a parent property). Let's say I don't want this page to be a "regular" category page, but a custom page; maybe it's not even at an URL like /categories, but something like /browse-offers. If I make a page (by creating both content/browse-offers.md and layouts/_default/browse-offers.html), how can I access the list of categories? Should I make it the "list" page of the "categories" section instead? What if the "list" page of the categories is supposed to be a filtered list of all categories instead?

I got the idea that Hugo was extremely flexible, but all the documentation and the examples I can find always talk about a plain posts/list-of-posts site structure. What if I wanted to show a gallery of "top offers" in the homepage? I can't find how to get a list of "things" unless I do it in a "list" page...

1 Answers

You can walk over the categories (in any layout) and check the 'parent' Parameter like this:

{{ range (where .Site.RegularPages "Section" "categories" ) }}
  {{ if .Params.parent }}
    ...
  {{ else }}
    ...
  {{ end }}
{{ end }}
Related