in my content directory /plumbers the page front matter have
details:
availability: available
featured: true
In layouts/index.html
# I want to tell Hugo that
{{ range where .Site.RegularPages "Section" "plumbers" }}
# and if the total of pages with .Params.details.featured equals or greater than 6
{{ if ge .Params.details.featured 6 }}
# then range the regular page from section plumber which have the param value featured: true
{{ range first 6 (where .Site.RegularPages "Section" "plumbers") }}
# and if the .Params.details.featured less then 6 but greater then 0 then take the what ever number of available pages with param value featured: true and add the other regular pages as normal to make a total of 6
{{ }}
# and if the .Params.details.featured is 0 means there are no pages with param value featured: true then just
{{ range first 6 (where .Site.RegularPages "Section" "plumbers") }}
After that, you’re dealing with one single page. And then you can’t count the pages where featured is set. Use a nested where to get a slice of pages where featured is set and check its len then
Something like that. I didn’t test it, though. You can combine the first and second where as described in the documentation.
{{ $plumbers:= where .Site.RegularPages "Section" "plumbers" }}
{{ $featured := where $plumbers "Params.details.featured" "eq" true }}
{{if gt 6 (len $featured)}}
Do your stuff here
{{ end }}
how can i make Hugo check that if $featured less than 6 and more then 0 then take what ever the number of $featured pages + where .Site.RegularPages “Section” “plumbers” }} and show a total of 6 .
Left as an exercise. I’m not going to do all the coding for you – there’s the documentation, and there are tons of examples in the forum. In my opinion, you learn more by trial and error then by someone writing your code for you.
Maybe. But how else are you going to leave your “beginner” state? By having someone else spending hours in the documentation and then feeding you the morsels?
Yes, what you want to do is possible. Though I don’t understand the requirement of the number of featured pages being > 0. Basically, you always want to show six pages, a maximum of which should be the featured ones.
in the 1st step I want hugo to check if the total number of pages with .Params.details.featured is equal or greater then 6
In the 2nd option I want Hugo to check if there is at least 1 page with value .Params.details.featured and total less then 6 then I want Hugo to take what ever the amount of featured pages available and combine with regular pages to show a total of 6 results.
in the 3rd option if there are no featured pages then just {{ range where .Site.RegularPages “Section” “plumbers” }}
the 2nd option is where I cannot wrap my head around on how to tell Hugo to first check and then combine featured and non featured pages to get to a total of 6 results.
You’re thinking way too complicated – there are no three cases. You simply want to show six pages, as many of them as possible being featured ones. Range over the featured pages. If you reach six, you’re done. Otherwise continue with the other pages…
I know I am making it complicated but cannot find the apropriate solution how I tell Hugo that range section with pages which have value featured true if get 6 perfect else continue to add more pages until the total is 6.
How you translate it to Hugo code? that I can use in layout.
Ok, I’m sure there is a more straight forward way but this is what I came up with:
{{/* Initial value for those_six */}}
{{ $those_six := first 6 $all_plumbers }}
{{/* I'll grab a maximum of 6 featured entries */}}
{{ $featured := first 6 (where $plumbers ".Params.details.featured") }}
{{/* Complement will remove the featured items from the $all_plumbers */}}
{{ $not_featured = complement $featured $all_plumbers }}
{{/* If we found at least one featured */}}
{{ with $featured }}
{{ $featured_number := len $featured }}
{{/* If we have less than 6 */}}
{{ if lt $featured_number 6 }}
{{/* I substract from 6 the numbers of available featured */}}
{{ $numbers_of_not_featured_i_need := sub 6 $featured_number }}
{{/* I can replace those six with an array containing $featured + the first x iem of the not featured pool */}}
{{ $those_six = append $featured (first $numbers_of_not_featured_i_need $not_featured) }}
{{ else }}
{{/* We have 6 featured so we can use them all for those_six */}}
{{ $those_six = $featured }}
{{ end }}
{{ end }}
Your example was really helpful in learning a new method. As in all the 3 condition I am calling the partial card, maybe I just apply conditions only to before calling partial.