# Range data file but only through selected items

**URL:** <https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584>\
**Category:** support\
**Created:** [April 17, 2018, 2:33pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584 "2018-04-17T14:33:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zde](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@Zde](https://discourse.gohugo.io/u/Zde)\
**Post date:** [April 17, 2018, 2:33pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/1 "2018-04-17T14:33:26Z")

</div>

I try to find solution in discourse but nothing found.  
I have similar yaml data file like this: `products.yaml`

```auto
- name: product1
  price: 10
- name: product2
  price: 50
- name: product3
  price: 2  
- name: product4
  price: 23
etc.

```

On every page, I render table with some products (different products and order of products).  
How to `range` only through products I want to use in table ?  
Looking for something like this:

```auto
{{ range .Site.Data.products "product7" "product21" "product15" "product3" }}
.. html code for table ..
{{ end }}

```

Thank you guys

---

<div class="post-metadata">

**Author:** ![Bin951](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bin951/32/5003_2.png) [@Bin951](https://discourse.gohugo.io/u/Bin951)\
**Post date:** [April 17, 2018, 2:55pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/2 "2018-04-17T14:55:13Z")

</div>

> [@Zde](#):
>
> {{ range .Site.Data.products “product7” “product21” “product15” “product3” }}  
> … html code for table …  
> {{ end }}

is it possible to use

```auto
{{ with (index .Site.Data.products "product7" "product21" "product15" "product3" }}
   ...do some stuff here 
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 17, 2018, 2:59pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/3 "2018-04-17T14:59:14Z")

</div>

> [@Bin951](#):
>
> {{ with (index .Site.Data.products “product7” “product21” “product15” “product3” }}  
> …do some stuff here  
> {{ end }}

Not sure but:

```auto
{{ range (slice "product7" "product21" "product15" "product3") }}
   {{ with (index Site.Data.products .) }}
       do something here, the dot is your value)
   {{ end }}
{{ end }}

```

Should work.

---

<div class="post-metadata">

**Author:** ![Mikhail](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mikhail/32/1138_2.png) [@Mikhail](https://discourse.gohugo.io/u/Mikhail)\
**Post date:** [April 17, 2018, 3:08pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/4 "2018-04-17T15:08:55Z")

</div>

In the Front Matter of page:

```auto
product: ["product7", "product21", "product15", "product3"]

```

In the template:

```auto
{{ range where "Site.Data.products" "intersect" "Params.product" }}
{{ .name }}{{ .price }}
{{ end }}

```

Not tested.

---

<div class="post-metadata">

**Author:** ![Zde](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@Zde](https://discourse.gohugo.io/u/Zde)\
**Post date:** [April 18, 2018, 9:15am UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/5 "2018-04-18T09:15:55Z")

</div>

@Bin951 @regis @Bin951 I have tried to implement all your three suggestions for a few hours but without success.

@Mikhail this is exactly concept what I am looking for. Put name of selected products to Front Matters of `.md` file and generate a table in the template from them. Unfortunately code is not working for me right now.

But I will take any other solution if it works.  
Any advice ? Thanks.

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 18, 2018, 11:55am UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/6 "2018-04-18T11:55:21Z")

</div>

I think @Mikhail suggestion needs one correction. Could you try it with:

```auto
{{ range where ".Site.Data.products" "intersect" .Params.product }}

```

`.Params.product` instead of `"Params.product"` and `".Site.Data.products"` instead of `"Site.Data.products"`

But then again, it is untested. We will need a repo of the project for further help. Cheers.

---

<div class="post-metadata">

**Author:** ![cmal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cmal/32/5324_2.png) [@cmal](https://discourse.gohugo.io/u/cmal)\
**Post date:** [April 18, 2018, 1:42pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/7 "2018-04-18T13:42:16Z")

</div>

Sorry for my previous post, i missed some background.

You’re looking for the name key in your `.Site.Data.products` so `"name"` needs to appear before intersect. However, `.Site.Data.products` should not have quotation marks either, because it’s a variable, like `.Params.product`.

```auto
{{ range where .Site.Data.products "name" "intersect" .Params.product }}

```

See the [where docs](https://gohugo.io/functions/where/) 🙂

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 18, 2018, 1:51pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/8 "2018-04-18T13:51:58Z")

</div>

I’ve got this image of us all perplexly pointing at @Zde’s screen with confusing suggestions 🙂 We’re getting there though!

---

<div class="post-metadata">

**Author:** ![Zde](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@Zde](https://discourse.gohugo.io/u/Zde)\
**Post date:** [April 18, 2018, 4:09pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/9 "2018-04-18T16:09:36Z")

</div>

@cmal your wide explanation is perfectly clear and gave me some new pieces to this puzzle. I’ve tested it for a few hours but still no success.

@cmal @regis I create repo for this example project: [https://github.com/zdenekpribyla/hugo-range-over-selected-data](https://github.com/zdenekpribyla/hugo-range-over-selected-data)  
There are two spots where I test a `range`: `myPartial` and `myShortcodes`. Default range code is now `{{ range .Site.Data.products }}` (range through all products…).  
Could you guys check the repo? Maybe there is problem with path to `.yaml` or `.Params`.  
Thanks

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 18, 2018, 4:22pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/11 "2018-04-18T16:22:15Z")

</div>

Ok I’m sorry I misread your initial post and was under the asumption that “product1” was the key of the whole product, and not just its name. So:

```auto
{{ $include := (slice "product1" "product3") }}
{{ range where .Site.Data.products ".name" "in" $include }}
     <li><b>{{ .name }}</b> and price: {{ .price }}</li>
{{ end }}

```

You could easily retrieve $include from a Front Matter param.

---

<div class="post-metadata">

**Author:** ![cmal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cmal/32/5324_2.png) [@cmal](https://discourse.gohugo.io/u/cmal)\
**Post date:** [April 19, 2018, 9:30am UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/12 "2018-04-19T09:30:22Z")

</div>

Woops, you’re actually more correct than i was. “intersect” in this case would mean `.Site.Data.products` items can have many names that can intersect with `.Params.product`.

However, there’s only one name per product entry, so `"in"` in more appropriate as you pointed out @regis :

```auto
{{ range where .Site.Data.products "name" "in" .Params.product }}

```

---

<div class="post-metadata">

**Author:** ![Zde](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@Zde](https://discourse.gohugo.io/u/Zde)\
**Post date:** [April 19, 2018, 5:11pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/13 "2018-04-19T17:11:16Z")

</div>

Thank you guys @cmal @regis both solutions works.  
I update [example project repo](https://github.com/zdenekpribyla/hugo-range-over-selected-data)

Now there are two issues I fight with.

1. in output: products are ordered by order in `.yaml` file. And I need them get in order as they are inserted. For example:

```auto
in .md file
---
product: ["product4", "product1", "product3"]
---

```

But output is reordered and looks like:

- **product1**
- **product2**
- **product3**

1. it seems that `myShortcode.html` (shortcode) does not work with `.Params.product`  
I mean if I use `.Params.product` in this part of code:  
`{{ range where .Site.Data.products "name" "in" .Params.product }}`  
is it hugo feature or bad syntax ?

Thanks again 🖖

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 19, 2018, 5:20pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/14 "2018-04-19T17:20:53Z")

</div>

From whithin your shortcode, you must access the page context using `$.Page`, so you might try `$.Page.Params.product` and `$.Page.Site.Data.products`.

For your yaml ordering, I think its a go `map/slice` limitation 😕 Not sure what can be done, maybe someone else can find a workaround.

@kaushalmodi usually knows best about such topics 🙂

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [April 19, 2018, 5:26pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/15 "2018-04-19T17:26:31Z")

</div>

> [@regis](#):
>
> $.Page.Site.Data.products

Hmm, I have never used that before… wouldn’t `$.Site.Data.products` be the same?

> @kaushalmodi usually knows best about such topics

heh, I don’t have a fancy solution for the ordering… I just prefix the items with numbers in a peculiar format (like `"01.foo"`, `"02.bar"`, …) and then just use regex to remove the number prefix (`{{ replaceRE $var "^[0-9]+\\." "" }}`). I use that trick for ordering menu items without having to save weights in the site config, ordering Data file items, etc.

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [April 19, 2018, 5:32pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/16 "2018-04-19T17:32:22Z")

</div>

> [@kaushalmodi](#):
>
> Hmm, I have never used that before… wouldn’t $.Site.Data.products be the same?

not sure, maybe.

---

<div class="post-metadata">

**Author:** ![Zde](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@Zde](https://discourse.gohugo.io/u/Zde)\
**Post date:** [April 20, 2018, 10:33am UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/17 "2018-04-20T10:33:49Z")

</div>

## Summary:

Range over `.Site.Data.products` (products.yaml) only with products specified in **front matter:**

```auto
---
products: ["product1", "product2"]
---
{{< shortcode >}}

```

for **partial.html**

```auto
{{ range where .Site.Data.products ".name" "in" .Params.product }}
   .. do some stuff here ..
{{ end }}

```

for **shortcode.html**

```auto
{{ range where .Site.Data.products ".name" "in" $.Page.Params.product }}
   .. do some stuff here ..
{{ end }}

```

Entire code on [GitHub](https://github.com/zdenekpribyla/hugo-range-over-selected-data)

`Sort` is more difficult. I will probably use some kind of other value `rating` in .yaml and sort like this:  
`{{ range sort (where .Site.Data.products ".name" "in" $.Page.Params.product) "rating" "desc" }}`

Thank you guys for help (and generally your help in Hugo community) 🕶

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [April 20, 2018, 11:27am UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/18 "2018-04-20T11:27:54Z")

</div>

> [@Zde](#):
>
> for shortcode.html
> 
> {{ range where .Site.Data.products

I’m surprised that that worked…

Should it be `$.Site. ..`?

---

<div class="post-metadata">

**Author:** ![Zde](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@Zde](https://discourse.gohugo.io/u/Zde)\
**Post date:** [April 20, 2018, 11:58am UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/19 "2018-04-20T11:58:40Z")

</div>

it works just like: `{{ range where .Site.Data.products..`

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [April 20, 2018, 12:08pm UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/20 "2018-04-20T12:08:54Z")

</div>

Thanks. That’s good to know.

_I just stick by the rule of thumb of always using the root context `$` within shortcodes._

---

<div class="post-metadata">

**Author:** ![Zde](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@Zde](https://discourse.gohugo.io/u/Zde)\
**Post date:** [February 1, 2019, 7:11am UTC](https://discourse.gohugo.io/t/range-data-file-but-only-through-selected-items/11584/21 "2019-02-01T07:11:41Z")

</div>

Hello guys  
in my previous issue we found the solution for iteration `products name in data file products.yaml` where iterated **name** ’s were defined in `front matter` (see front matter - products):

```auto
Page front matter:
---
Title: Table with selected products 
layout: whatever
product: ["product001", "product005", "product011"]
---

```

Solution:

```auto
{{ range where .Site.Data.products ".name" "in" .Params.product }}
   .. do some stuff here ..
{{ end }}

```

Well, now I am looking for similar syntax but for `.Pages`.  
So every page has `.Params` **pageId** and **relatedPages** in front matter.  
And I need iterate over all **pageId** ’s (all pages) which are defined in front matter in **relatedPages** like this:

```auto
---
Title: How to fix a car
pageId: 002
relatedPages: ["001", "003", "007", "012"]
---

```

I thought I can retrieve the code from previous syntax. Something like this:

```auto
{{ range where .Site.Data.Pages ".Params.pageId" "in" .Params.relatedPages }}
   <a href="{{ .URL }}">{{ .Title }}</a><br>
{{ end }}

```

I have tried lots of syntax combination but it does not work. Also I did not find similar example in discourse. Any advice ?  
Thank you.
