I want to include a list of all other products on a single product page (layouts/product/single.html).
I was hoping I could do something like this in the single template file:
{{ range where .Data.Pages “Type” “product” }}
{{ .Render “summary”}}
{{ end }}
However this will only work in the homepage template and not inside a layout template. (is why explained in the docs because I’m not getting it?)
Attempted Solutions
{{ .Render “summary” }} will render the current item. Is there a function that will list all other content items? After looking through the documentation the closest I can come up with is {{ .Prev.Title }} to list the previous item.
Moving the content files to data directory I can achieve this with the following:
{{ range .Site.Data.product }}
{{ .Title }}
{{ end }}
However I would like to reference the items stored in content/product/
I am new to Hugo and I am probably missing a key point but hopefully this post will help me (and others) to grasp a better understanding of the Hugo template system.
If I understood your questions correctly, you want to list all pages of type “product” on a page (accompanied with something about them like summary or product description).
I did a similar thing a while ago while creating an archive page for my pages of type post, it is documented here. Is this what you are looking for?
@parsiya yes but instead of an archive page I would like to display the list of other products within the single.html template of a product.
The ‘Recent’ variable is displaying what I want with the following code:
{{ range first 4 (where .Site.Recent “Type” “product”)}}
{{ .Render “list” }}
{{ end }}
It is confusing that ‘Recent’ works inside a layout template and not ‘Pages’ whereas in the homepage ‘Pages’ works just fine.
Now I need to add a filter to remove (this) product from displaying on the single.html template. Think of your recent posts list but excluding the current post from this list.
If you use Recent you should upgrade to Hugo 0.15. I don’t see how .Pages could work on the home page (maybe your old Hugo version …), but if you use these you should be safe all over:
.Site.Pages => for all pages
.Data.Pages => all pages for that Node (not applicable for single pages; think taxonomy/list pages).