Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate static site from JSON array

I have a giant JSON array of every US state and city, along with other data about each of them. I'd like to iterate over the JSON and output a tree structure like this:

  1. [Alabama]
    • index.html
    • [Abbeville]
      • index.html
    • [Adamsville]
      • index.html
  2. [Alaska]
    • index.html
    • [Anchorage]
      • index.html
    • [Fairbanks]
      • index.html
  3. ...etc

I'd have two layouts:

  1. state.html
  2. city.html

So far, I haven't found a great way to do this. A lot of static gens seem to have the ability to use JSON for meta data within content, but not for the primary source of content.

thanks!

like image 405
noahlocke Avatar asked Apr 28 '26 16:04

noahlocke


1 Answers

The Middleman static site generator supports this. You use their dynamic pages to create a list of the pages to create. The data comes from their data file feature. Here are the links to the pages explaining these

https://middlemanapp.com/advanced/dynamic_pages/ https://middlemanapp.com/advanced/data_files/

You would do something like have states.yml containing the state and city data

- states
    - name: Alabama
      cities:
        - name: Abbeville
          pop: X
        - name: Adamsville
          pop: Y

and then create the proxy pages e.g.

data.states.each do |state|
  proxy "/#{state.name}/index.html", "templates/state.html", :locals => {state: state}
  state.cities.each do |city|
    proxy "/#{state.name}/#{city.name}/index.html", "/templates/city.html", :locals => {state: state, city: city}
  end
end
like image 165
Gerard Condon Avatar answered Apr 30 '26 21:04

Gerard Condon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!