Need help with Hugo greater equal conditions

Hi there,

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") }}

How can I achieve this?

Thanks

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

Hi @chrillek

Thanks for taking the time, can you be bit more specific in the solution you provided. can you please dumb it down to the beginner level solution?

Thanks

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 }}
1 Like

Thanks @chrillek

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 .

Thank you

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.

1 Like

Hi @chrillek

I am going to spend hours in the documentation but I just need a confirmation if it is actually possible in Hugo the second step which is to

Thanks

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.

Hi @chrillek

There are 3 steps to this

  1. 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

  2. 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.

  3. 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.

Thanks

Hi all,

still not able to solve this issue in Hugo

Any tip or trick will be greatly appreciated.

Thanks

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…

1 Like

Hi @chrillek

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.

Thanks again for taking the time

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 }}
1 Like

Thank you @regis for providing a solution.

I am unable to understand this code, what you mean by where $plumbers?

I define the $all_plumbers

{{ $all_plumbers:= where .Site.RegularPages "Section" "plumbers" }}
{{ $those_six := first 6 $all_plumbers }}

but then what you mean by where $plumbers? where does this come from, I mean how Hugo know what $plumbers?

Your code is very detailed with comments and directions but with my brain on the wall, where did I ask Hugo to range the pages?

For example the simple code I am using now is this

{{ range first 6 (where .Site.RegularPages "Section" "plumbers") }} 
{{ partial "components/card.html" . }} {{ end }}

Don’t I need to range first all pages?

is this actual code or is it a place holder where I call the partial?

Apologies in advance I know my questions will sound stupid :face_with_spiral_eyes: since you have added clear comments to every line.

should I just forget about this method of Hugo

{{ range first 6 (where .Site.RegularPages "Section" "plumbers") }} 
{{ partial "components/card.html" . }} {{ end }}

or the solution provided actually fit in this Hugo code type?

Thanks

Typo, this should be where $all_plumbers.

This code should work as is, but I don’t have any content model to test it against.

1 Like

Thanks for clarifying that @regis

At what stage in this code I call the partial?

I am only familiar with using partial like this

Where does it fit in the code?

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.

Thanks

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