Merge pull request #2369 from jekyll/json-data

This commit is contained in:
Matt Rogers 2014-05-09 12:33:48 -05:00
commit 50065a2781
5 changed files with 37 additions and 4 deletions

View File

@ -33,6 +33,18 @@ Feature: Data
And I should see "Jack" in "_site/index.html" And I should see "Jack" in "_site/index.html"
And I should see "Leon" in "_site/index.html" And I should see "Leon" in "_site/index.html"
Scenario: autoload *.json files in _data directory
Given I have a _data directory
And I have a "_data/members.json" file with content:
"""
[{"name": "Jack", "age": 28},{"name": "Leon", "age": 34}]
"""
And I have an "index.html" page that contains "{% for member in site.data.members %}{{member.name}}{% endfor %}"
When I run jekyll build
Then the "_site/index.html" file should exist
And I should see "Jack" in "_site/index.html"
And I should see "Leon" in "_site/index.html"
Scenario: autoload *.yml files in _data directory with space in file name Scenario: autoload *.yml files in _data directory with space in file name
Given I have a _data directory Given I have a _data directory
And I have a "_data/team members.yml" file with content: And I have a "_data/team members.yml" file with content:
@ -62,4 +74,3 @@ Feature: Data
Then the "_site/index.html" file should exist Then the "_site/index.html" file should exist
And I should see "Jack" in "_site/index.html" And I should see "Jack" in "_site/index.html"
And I should see "Leon" in "_site/index.html" And I should see "Leon" in "_site/index.html"

View File

@ -198,7 +198,7 @@ module Jekyll
base = File.join(source, dir) base = File.join(source, dir)
return unless File.directory?(base) && (!safe || !File.symlink?(base)) return unless File.directory?(base) && (!safe || !File.symlink?(base))
entries = Dir.chdir(base) { Dir['*.{yaml,yml}'] } entries = Dir.chdir(base) { Dir['*.{yaml,yml,json}'] }
entries.delete_if { |e| File.directory?(File.join(base, e)) } entries.delete_if { |e| File.directory?(File.join(base, e)) }
entries.each do |entry| entries.each do |entry|

View File

@ -10,7 +10,7 @@ In addition to the [built-in variables](../variables/) available from Jekyll,
you can specify your own custom data that can be accessed via the [Liquid you can specify your own custom data that can be accessed via the [Liquid
templating system](http://wiki.github.com/shopify/liquid/liquid-for-designers). templating system](http://wiki.github.com/shopify/liquid/liquid-for-designers).
Jekyll supports loading data from [YAML](http://yaml.org/) files located in the Jekyll supports loading data from [YAML](http://yaml.org/) and [JSON](http://www.json.org/) files located in the
`_data` directory. `_data` directory.
This powerful feature allows you to avoid repetition in your templates and to This powerful feature allows you to avoid repetition in your templates and to
@ -22,7 +22,7 @@ Plugins/themes can also leverage Data Files to set configuration variables.
As explained on the [directory structure](../structure/) page, the `_data` As explained on the [directory structure](../structure/) page, the `_data`
folder is where you can store additional data for Jekyll to use when generating folder is where you can store additional data for Jekyll to use when generating
your site. These files must be YAML files (using either the `.yml` or `.yaml` your site. These files must be YAML files (using either the `.yml`, `.yaml` or `.json`
extension) and they will be accessible via `site.data`. extension) and they will be accessible via `site.data`.
## Example: List of members ## Example: List of members

View File

@ -0,0 +1,12 @@
[
{
"name": "Jack",
"age": 27,
"blog": "http://example.com/jack"
},
{
"name": "John",
"age": 32,
"blog": "http://example.com/john"
}
]

View File

@ -378,6 +378,16 @@ class TestSite < Test::Unit::TestCase
assert_equal site.site_payload['site']['data']['languages'], file_content assert_equal site.site_payload['site']['data']['languages'], file_content
end end
should 'auto load json files' do
site = Site.new(Jekyll.configuration)
site.process
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'members.json'))
assert_equal site.data['members'], file_content
assert_equal site.site_payload['site']['data']['members'], file_content
end
should "load symlink files in unsafe mode" do should "load symlink files in unsafe mode" do
site = Site.new(Jekyll.configuration.merge({'safe' => false})) site = Site.new(Jekyll.configuration.merge({'safe' => false}))
site.process site.process