Range, if statement and first

I want to get the first 10 products that meet the condition {{if in .URL "boston"}}

If I do the following, I get all products that meet the condition.

{{range .Site.RegularPages}}
{{if in .URL "boston"}}
{{partial "banner.html" .}}
{{end}}
{{end}}

If I add, first 10 like below, it only gives 3 products as it limits the results in the range.

{{range first 10 .Site.RegularPages}}
{{if in .URL "boston"}}
{{partial "banner.html" .}}
{{end}}
{{end}}

I was thinking I need to apply first 10 after the if statement and do something like this, but I’m not sure what variable to use in the second range.

{{range .Site.RegularPages}}
{{if in .URL "boston"}}
{{range first 10 //not sure what to put here//}}
{{partial "banner.html" .}}
{{end}}
{{end}}
{{end}}

Have a look at range + where examples in the docs.

Thanks that helped.

To continue from the example above, I now have a more complex if statement which I can’t translate into a where statement.

I am looking to achieve the same behavior as shown above. Only range through the first 10 pages that meet the if statement. This is my code:

{{range first 10 .Site.RegularPages}}
{{if and (or (in .Params.locations (index $fileName 0)) (eq .Params.filter (index $fileName 0)) (in .Params.type (index $fileName 0)))}}
{{partial "banner.html" .}}
{{end}}
{{end}}

I think nested where is what you’re looking for. Give it a try and let us know if you encounter problems :slight_smile:

I’m having difficulties with this. Not sure how to translate the and or statements with the nested where statements. Any help is appreciated.

If you can share your repo, it might be easier to reproduce. This is a wild, untested guess, but did you try something like:

{{ range first 10 (where .Site.RegularPages (in ".URL" "boston")) }}
   {{ partial "banner.html" . }}
{{ end }}

or

{{ range first 10 (where .Site.RegularPages (in .URL "boston")) }}
   {{ partial "banner.html" . }}
{{ end }}

or

{{ range first 10 (where in .Site.RegularPages.URL "boston") }}
   {{ partial "banner.html" . }}
{{ end }}

Thanks Rick. I tried your constructs, but none of them worked.

I was able to solve it by not using a where statement and just using .Params.locations instead of .URL

But I’m stuck on how to do range first with this snippet:

{{range first 10 .Site.RegularPages}}
{{if and (or (in .Params.locations (index $fileName 0)) (eq .Params.filter (index $fileName 0)) (in .Params.type (index $fileName 0)))}}
{{partial "banner.html" .}}
{{end}}
{{end}}

Well the and part of your if is a simple nested where, but for the or part I have no idea how to implement it. I think you should first take a deep look at the whole where documentation, then think about how to simplify your query.

What is it precisely that you’re trying to achieve? Can’t you do it simpler? If you explain with details (and with example frontmatters) maybe we can figure out something :slight_smile:

But it’s a bit hard to figure out what you’re trying to do if we don’t know what $fileName and the params are supposed to look like :wink:

It’s a lot about trial and error. But I’m afraid no one will have the time to rebuild your environment from scratch in order to help you.

You will have to provide a repo with your full Hugo project so we can easily clone it, and play around with it.

Understood. Point taken.

I’m fairly new to Hugo and coding in general. I usually spend hours on a problem and that includes reading the docs before asking on here. This is sort of a last resort. I guess what I fail to consider sometimes is that a code snippet is not enough. I often assume that the issue I’m experiencing can easily be solved by someone more experienced. But I get it.

In this instance, I can’t share the repo, but I’ve gathered that I should try and look at simplifying my code.

Thanks to everyone on here for helping.

1 Like

Some time it can, but when it’s not, access to a repo make it very easy to try and help, and it’s often fun. We’re answering those kind of threads because we like the challenge. But the absence of a repo often takes all the fun away.

Thanks for understanding.