# Access programmatically generated pages via site.pages?

**URL:** <https://talk.jekyllrb.com/t/access-programmatically-generated-pages-via-site-pages/3121>\
**Category:** Help\
**Created:** [July 25, 2019, 4:36pm UTC](https://talk.jekyllrb.com/t/access-programmatically-generated-pages-via-site-pages/3121 "2019-07-25T16:36:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![araphiel](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@araphiel](https://talk.jekyllrb.com/u/araphiel)\
**Post date:** [July 25, 2019, 4:36pm UTC](https://talk.jekyllrb.com/t/access-programmatically-generated-pages-via-site-pages/3121/1 "2019-07-25T16:36:49Z")

</div>

**Preface**

I’m working on a small plugin that fetches JSON data from an API, then directly creates pages for each entry

**Issue**

This all builds perfectly but my issue:

- On the generated page, I **can** access any page variables perfectly - {{ page.foobar }}
- On other pages, I **cannot** loop through the custom pages to create a list - `{% for page in site.pages %}`

Looking at the generator section of code, this should work but I don’t know if I am missing something.

```
module Jekyll
    module CUSTOM_REST_API 
        def get_data
            # This function returns an array of hashes, containing my API data
            data
        end
    end

    class CustomGenerator < Generator
        include CUSTOM_REST_API

        def generate(site)
            content = get_data
            content.each do | data |
                # SEE HERE - Add each custom page into the site.pages array to be built
                site.pages << CustomPage.new(site, site.source, 'custom', data)
            end
        end
    end

    class CustomPage < Page
        def initialize(site, base, dir, datum)
          @site = site
          @base = base
          @dir = dir
          @name = datum["slug"] + "/index.html"

          # Set generated filepath name
          self.process(@name)

          # Use custom page layout / template
          self.read_yaml(File.join(base, '_layouts'), 'custom.html')

          # Add custom page data to each page
          self.data['title'] = datum["name"]
          self.data['category'] = "custom"
          self.data['foobar'] = datum["foobar"]
        end
      end
end
```

---

<div class="post-metadata">

**Author:** ![araphiel](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@araphiel](https://talk.jekyllrb.com/u/araphiel)\
**Post date:** [July 25, 2019, 7:04pm UTC](https://talk.jekyllrb.com/t/access-programmatically-generated-pages-via-site-pages/3121/2 "2019-07-25T19:04:01Z")

</div>

I’m assuming the generate function runs after an inventory is taken?

I believe this is the class that registers everything in site.pages, however I don’t know if this should belong here.

> <https://github.com/jekyll/jekyll/blob/9a4b74c44b9679803d907fd97abbd6a84e671c31/lib/jekyll/reader.rb>

---

<div class="post-metadata">

**Author:** ![araphiel](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@araphiel](https://talk.jekyllrb.com/u/araphiel)\
**Post date:** [August 5, 2019, 4:58pm UTC](https://talk.jekyllrb.com/t/access-programmatically-generated-pages-via-site-pages/3121/3 "2019-08-05T16:58:04Z")

</div>

**Recap:**  
I created pages from an external API using a custom plugin.  
The plugin itself uses a custom `generator` to transform and build pages based on data.

On the data pages, I could access page data `{page.custom_property}`  
Outside of the data pages, I could not access this data from `site.pages`  
This is because the standard `site.pages` is already set by the time this generator runs.

**Solution:**  
In my plugin, I simply added my page data to `site.data` instead, since it is accessible at anytime.

---

<div class="post-metadata">

**Author:** ![vlev322](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/vlev322/32/2642_2.png) [@vlev322](https://talk.jekyllrb.com/u/vlev322)\
**Post date:** [July 2, 2020, 12:55pm UTC](https://talk.jekyllrb.com/t/access-programmatically-generated-pages-via-site-pages/3121/4 "2020-07-02T12:55:45Z")

</div>

hello Recap, could you write down the final part of your plugin that generated page from some data?
