# Can't access .Data.Pages (or .Site.Pages) inside another range

**URL:** https://discourse.gohugo.io/t/cant-access-data-pages-or-site-pages-inside-another-range/15512
**Category:** support
**Created:** [November 27, 2018, 9:36am UTC](https://discourse.gohugo.io/t/cant-access-data-pages-or-site-pages-inside-another-range/15512 "2018-11-27T09:36:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![MattG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattg/32/7402_2.png) [@MattG](https://discourse.gohugo.io/u/MattG)
#### Post date: [November 27, 2018, 9:36am UTC](https://discourse.gohugo.io/t/cant-access-data-pages-or-site-pages-inside-another-range/15512/1 "2018-11-27T09:36:21Z")

</div>

Hi All,  
I’m new to Hugo but loving it so far! I’ve hit a problem that I can’t seem to get around though.

I’m trying to build a homepage that has various sections or blocks that are optionally displayed according to configuration in the page’s front matter (I’m setting this up using [forestry.io](http://forestry.io) and using their front matter templates to make this configurable for an end user)

I’ve recreated the issue in a test site. The problem is illustrated by this template I’m using for the home page:

```
{{ partial "header.html" . }}
<h1>HOMEPAGE TEMPLATE</h1>
<section>
    <h1>{{ .Title }}</h1>
    {{ .Content }}
    {{ range .Params.blocks }}
        {{ if eq .template "home-page-riding-packages" }}
            <h2>This range gives an error... 'can't iterate over &lt;nil&gt;'</h2>
            {{ range where .Data.Pages "Section" "packages" }}
                {{ .Title }}
            {{ end }}
        {{ end }}
    {{ end}}

    <h2>This range works...</h2>
    {{ range where .Data.Pages "Section" "packages" }}
        <p>{{ .Title }} - {{ .Params.custom1 }}</p>
    {{ end }}

</section>
{{ partial "footer.html" . }}  

```

I’m using ‘range’ to iterate over the blocks and then checking the template value to know whether to display the block or not. If I try to range over .Sites.Pages or .Data.Pages. within this outer range I get an error saying that I can’t iterate over nil - the list of pages is empty. Further down in the example, outside of the outer range, I can list the pages and they are displayed correctly.

I’m guessing that the outer ‘range .Params.blocks’ call is rebinding the scope of . to the block in my data, which is why the .Data.Pages or .Sites.Pages doesn’t then work.

Anyone have any ideas how I can do this? I’ve been trying all sorts of things to try and fix this.

Any help would be greatly appreciated - I’ve been stuck on this since yesterday and I’m rapidly running out of hair to pull out 😉

Thanks,  
Matt

---

<div class="post-metadata">

### Author: ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)
#### Post date: [November 27, 2018, 9:56am UTC](https://discourse.gohugo.io/t/cant-access-data-pages-or-site-pages-inside-another-range/15512/2 "2018-11-27T09:56:14Z")

</div>

> [@MattG](#):
>
> {{ range where .Data.Pages “Section” “packages” }}

Try:

```auto
{{ range where $.Data.Pages "Section" "packages" }}

```

Also note that when you do “section filtering” in a where clause, I suspect that you will always want something ala:

```auto
{{ range where $.Site.RegularPages "Section" "packages" }}

```

---

<div class="post-metadata">

### Author: ![MattG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattg/32/7402_2.png) [@MattG](https://discourse.gohugo.io/u/MattG)
#### Post date: [November 27, 2018, 10:00am UTC](https://discourse.gohugo.io/t/cant-access-data-pages-or-site-pages-inside-another-range/15512/3 "2018-11-27T10:00:49Z")

</div>

I’m sure that was one of the things I tried… but adding the $ works. Thanks for that - you’re my hero!

---

<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: [November 27, 2018, 12:38pm UTC](https://discourse.gohugo.io/t/cant-access-data-pages-or-site-pages-inside-another-range/15512/4 "2018-11-27T12:38:49Z")

</div>

This may clarify things: [https://regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/](https://regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/)

---

<div class="post-metadata">

### Author: ![MattG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattg/32/7402_2.png) [@MattG](https://discourse.gohugo.io/u/MattG)
#### Post date: [November 27, 2018, 4:41pm UTC](https://discourse.gohugo.io/t/cant-access-data-pages-or-site-pages-inside-another-range/15512/5 "2018-11-27T16:41:20Z")

</div>

Thanks so much that article is super helpful.
