【问题标题】:Generate static site from JSON array从 JSON 数组生成静态站点
【发布时间】:2021-05-11 08:44:10
【问题描述】:

我有一个包含美国每个州和城市的巨大 JSON 数组,以及每个州和城市的其他数据。我想遍历 JSON 并输出这样的树结构:

  1. [阿拉巴马州]
    • index.html
    • [阿布维尔]
      • index.html
    • [亚当斯维尔]
      • index.html
  2. [阿拉斯加]
    • index.html
    • [安克雷奇]
      • index.html
    • [费尔班克斯]
      • index.html
  3. ...等

我有两种布局:

  1. state.html
  2. city.html

到目前为止,我还没有找到一个很好的方法来做到这一点。许多静态生成器似乎能够将 JSON 用于内容中的元数据,但不能用于内容的主要来源。

谢谢!

【问题讨论】:

标签: middleman static-site hugo


【解决方案1】:

Middleman 静态站点生成器支持这一点。您使用他们的动态页面来创建要创建的页面列表。数据来自他们的数据文件功能。以下是解释这些页面的链接

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

你会做一些事情,比如让 states.yml 包含州和城市数据

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

然后创建代理页面,例如

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

【讨论】:

    猜你喜欢
    • 2011-02-20
    • 2021-09-29
    • 2013-05-04
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2020-11-21
    • 2021-04-05
    • 2015-07-22
    相关资源
    最近更新 更多