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