diff --git a/features/data.feature b/features/data.feature
new file mode 100644
index 00000000..33adfaad
--- /dev/null
+++ b/features/data.feature
@@ -0,0 +1,65 @@
+Feature: Data
+ In order to use well-formatted data in my blog
+ As a blog's user
+ I want to use _data directory in my site
+
+ Scenario: autoload *.yaml files in _data directory
+ Given I have a _data directory
+ And I have a "_data/products.yaml" file with content:
+ """
+ - name: sugar
+ price: 5.3
+ - name: salt
+ price: 2.5
+ """
+ And I have an "index.html" page that contains "{% for product in site.data.products %}{{product.name}}{% endfor %}"
+ When I run jekyll
+ Then the "_site/index.html" file should exist
+ And I should see "sugar" in "_site/index.html"
+ And I should see "salt" in "_site/index.html"
+
+ Scenario: autoload *.yml files in _data directory
+ Given I have a _data directory
+ And I have a "_data/members.yml" 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
+ 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
+ Given I have a _data directory
+ And I have a "_data/team members.yml" 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.team_members %}{{member.name}}{% endfor %}"
+ When I run jekyll
+ 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: should be backward compatible with site.data in _config.yml
+ Given I have a "_config.yml" file with content:
+ """
+ data:
+ - name: Jack
+ age: 28
+ - name: Leon
+ age: 34
+ """
+ And I have an "index.html" page that contains "{% for member in site.data %}{{member.name}}{% endfor %}"
+ When I run jekyll
+ 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"
+
diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb
index 56da1d11..3604f523 100644
--- a/features/step_definitions/jekyll_steps.rb
+++ b/features/step_definitions/jekyll_steps.rb
@@ -43,6 +43,12 @@ Given /^I have an? (.*) (layout|theme) that contains "(.*)"$/ do |name, type, te
end
end
+Given /^I have an? "(.*)" file with content:$/ do |file, text|
+ File.open(file, 'w') do |f|
+ f.write(text)
+ end
+end
+
Given /^I have an? (.*) directory$/ do |dir|
FileUtils.mkdir_p(dir)
end
diff --git a/jekyll.gemspec b/jekyll.gemspec
index f726d4d1..a9ddd338 100644
--- a/jekyll.gemspec
+++ b/jekyll.gemspec
@@ -59,6 +59,7 @@ Gem::Specification.new do |s|
bin/jekyll
cucumber.yml
features/create_sites.feature
+ features/data.feature
features/drafts.feature
features/embed_filters.feature
features/include_tag.feature
@@ -203,6 +204,7 @@ Gem::Specification.new do |s|
test/helper.rb
test/source/+/foo.md
test/source/.htaccess
+ test/source/_data/members.yaml
test/source/_includes/params.html
test/source/_includes/sig.markdown
test/source/_layouts/default.html
diff --git a/lib/jekyll/configuration.rb b/lib/jekyll/configuration.rb
index 43244b47..de903a96 100644
--- a/lib/jekyll/configuration.rb
+++ b/lib/jekyll/configuration.rb
@@ -10,6 +10,7 @@ module Jekyll
'destination' => File.join(Dir.pwd, '_site'),
'plugins' => '_plugins',
'layouts' => '_layouts',
+ 'data_source' => '_data',
'keep_files' => ['.git','.svn'],
'timezone' => nil, # use the local timezone
diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb
index 6084631a..5cad11c8 100644
--- a/lib/jekyll/site.rb
+++ b/lib/jekyll/site.rb
@@ -3,7 +3,7 @@ module Jekyll
attr_accessor :config, :layouts, :posts, :pages, :static_files,
:categories, :exclude, :include, :source, :dest, :lsi, :pygments,
:permalink_style, :tags, :time, :future, :safe, :plugins, :limit_posts,
- :show_drafts, :keep_files, :baseurl, :file_read_opts
+ :show_drafts, :keep_files, :baseurl, :data, :file_read_opts
attr_accessor :converters, :generators
@@ -56,6 +56,7 @@ module Jekyll
self.static_files = []
self.categories = Hash.new { |hash, key| hash[key] = [] }
self.tags = Hash.new { |hash, key| hash[key] = [] }
+ self.data = {}
if self.limit_posts < 0
raise ArgumentError, "limit_posts must be a non-negative number"
@@ -110,6 +111,7 @@ module Jekyll
def read
self.read_layouts
self.read_directories
+ self.read_data(config['data_source'])
end
# Read all the files in
_data
+
+ Well-formatted site data should be placed here. The jekyll engine will
+ autoload all yaml files (ends with .yml
or .yaml
)
+ in this directory. If there's a file members.yml
under the directory,
+ then you can access contents of the file through site.data.members
.
+
+
_site