Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow subdirectories in _data #2395

Merged
merged 3 commits into from
May 21, 2014
Merged

Conversation

rdumont
Copy link
Contributor

@rdumont rdumont commented May 11, 2014

Fixes #2082 - Support subdirectories in _data.

Subdirectories in the _data folder will also be scanned for .yaml, .yml and .json files, recursively. For more details, see the added Cucumber scenario and the examples below:

Using the following files:

_data/categories/dairy.yaml     => name: Dairy Products
_data/categories/spices.yaml    => name: Spices

Will result in:

categories:
  dairy:
    name: Dairy Products
  spices:
    name: Spices

@doktorbro
Copy link
Member

Great. Can you please add a note to the documentation in /site/docs/datafiles.md?

"""
name: Dairy Products
"""
And I have an "index.html" page that contains "{{ site.data.categories.dairy.name }}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I have a file _data/categories.yml that contains the value dairy: foobar? How is that handled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of how I list the entries in a directory (see line 213 of site.rb), directories will always override whatever is read from a file with the same name.

But this is due to a small detail in implementation. I think the behavior for such case should be considered unpredictable and people should be discouraged to name files the same as sub-directories. Just the same as they shouldn't have a .json and a .yaml with the same name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Can you please add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 😃

@rdumont rdumont closed this May 12, 2014
@rdumont rdumont reopened this May 12, 2014
@rdumont
Copy link
Contributor Author

rdumont commented May 12, 2014

I'm sorry, I accidentally clicked the close button.

@parkr
Copy link
Member

parkr commented May 12, 2014

Thanks for adding the docs. Can you please sprinkle in subdirectories in examples above the one you gave so it's more prominent? The entire Data Files doc page may require reworking.

@rdumont
Copy link
Contributor Author

rdumont commented May 14, 2014

@parkr Sorry, I didn't get what you meant by sprinkling in subdirectories. But yeah, I agree that with this change the entire Data Files doc page would greatly benefit from some rework.

@parkr
Copy link
Member

parkr commented May 15, 2014

Sorry, I didn't get what you meant by sprinkling in subdirectories

I just meant showing subdirectories and flat files shown side-by-side in the other examples on the page. :)

@benbalter Do you have any major thoughts on this?

@parkr parkr merged commit 3c6377d into jekyll:master May 21, 2014
parkr added a commit that referenced this pull request May 21, 2014
@doktorbro
Copy link
Member

Can’t wait to use it.

@parkr
Copy link
Member

parkr commented May 21, 2014

Jekyll 2.1 should be out early next week!

@rdumont
Copy link
Contributor Author

rdumont commented May 21, 2014

Great, thanks! I'll give some thought about the doc page too, so that we can improve it.

@rdumont rdumont deleted the data-subdirectories branch May 21, 2014 18:47
@fkostadinov
Copy link

I fail to run the given example for subdirectories. I am running Jekyll 2.1.0 on Windows 7 with the exact setup given here: http://jekyllrb.com/docs/datafiles/. Whereas the first example with a single members.yml works perfectly fine, the second example with multiple files in a subdirectory does not. Contents read out with {% for org in site.data.orgs %} and {{ org.name }} does not show anything. Interestingly, looping over all files in the subdirectory seems to recognize there are 2 files, because two entries are displayed. Is there anything missing in the examples (e.g. additional setup required in _config.yml, runtime arguments given to jekyll etc.)? Can someone please confirm the example works as given or provide an updated example? Thanks.

@parkr
Copy link
Member

parkr commented Jul 8, 2014

@fkastadinov You're right! The example is wrong – great catch. The way to remedy it is to say instead of org, org[1]. When Liquid iterates over Hashes (which is what site.data.orgs is for this subdir), it gets an array that looks like this:

[
["doeorg", {"username"=>"doeorg", "name"=>"Doe Org", "members"=>[{"name"=>"John Doe", "github"=>"jdoe"}]}],
["jekyll", {"username"=>"jekyll", "name"=>"Jekyll", "members"=>[{"name"=>"Tom Preston-Werner", "github"=>"mojombo"}, {"name"=>"Parker Moore", "github"=>"parkr"}]}]
]

org[0] is the key (sanitized filename), and org[1] is the value (the file's data).

@fkostadinov
Copy link

@parkr: Thanks, now everything works.

parkr added a commit that referenced this pull request Jul 9, 2014
@parkr
Copy link
Member

parkr commented Jul 9, 2014

@fkostadinov Can you please confirm that the change made in e8e245e works?

@fkostadinov
Copy link

@parkr: Yes, I tried the updated example, and it works! Perhaps you could also mention quickly that in the subdirectory example files are read out as key/value pairs and that org_hash[0] contains the filename (without ending) and org[1] the file's contents. This would help clarify things for an inexperienced user.

@jekyll jekyll locked and limited conversation to collaborators Feb 27, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support subdirectories in _data
5 participants