3

I'm looking for a good static site generator. I'm looking into either Jekyll / Octopress (Ruby) or maybe Phrozn or PieCrust (PHP). I only have one thing that holds me back:

How do I create a sidebar widget that reads data from a JSON file at build/compile time and generate a static page based from the data? A particular use case is when I have a JSON file of events and I want to show them on a static page as a list of events of the week on a sidebar.

I know I could simply use client-side Javascript to load and process that JSON file and then manipulate the DOM but I am sure that this is not a good idea.

Currently, I'm using PyroCMS and I placed this custom code as part of the template. (Not ideal, I know. Didn't bother to fix it since I'm planning to migrate away from PyroCMS anyway.)

I prefer to use Jekyll (really functional) or Phrozn or PieCrust (because I can reuse my previous PHP code) but I cannot see how to create a semi-dynamic page like the one I described above. I'm open to other static site generators as well -- as long as they are in PHP, Ruby, or Python.

Thank you in advance for you help.

4

2 回答 2

2

你可以编写 Jekyll 插件来做到这一点,可能是一个标签。如果将 JSON 放在名为 _data 的目录中,标签插件的骨架将类似于:

module Jekyll
  class JsonEventsTag < Liquid::Tag
    def initialize(tag_name, markup, tokens)
      super
        # Open & Parse JSON file somthing like
        @events = JSON.parse(File.read("_data/events.json"));
      end

      def render(context)
        # loop over @events, format into HTML and return the result
      end
    end
  end
end

Liquid::Template.register_tag('sidebar_events', Jekyll::JsonEventsTag)

然后,您可以将布局中的标签称为

{% sidebar_events %}

并且在构建站点时将替换为您的格式化来自 JSON 的列表。

于 2013-08-02T16:04:14.280 回答
0

经过更多研究,我发现 Phrozn 似乎可以做到。

http://www.phrozn.info/en/documentation/providers/</p>

不过,我希望听到更多来自 Jekyll 的观点。

于 2013-08-01T15:06:47.923 回答