Can a large YAML file (eg: data/artists.yml) be broken into smaller files within a folder (eg: data/artists/artist.yml)

I am building a Middleman/HAML site using dynamic pages and want to output many different profile pages from a profiles.yml file.

This ‘profiles’ YAML file has not only the profile username but also other data like image path, link, bio. The profiles file is getting very large and unmanageable, am I able to split this up into a folder of YAML files per profile?

eg:

data/profiles/name01.yml
data/profiles/name02.yml
data/profiles/name03.yml

etc…

and loop through these similarly to how I would loop through the large profiles.yaml file.

Thanks for any help on this.

References:

The docs say you can reference data files in folders. So given your folder structure (and if they contain a key called ‘title’) you could get the data with data.profiles.name01.title, data.profiles.name02.title and data.profiles.name03.title.

I’m not sure if you can loop through the folder dynamically but you should be able to do

names = data.profiles.name01 + data.profiles.name02 + data.profiles.name03

You may need to add a - at the top of your yaml files to create an extra object level and prevent keys with the same name from overwriting each other.

-
  title: "This is my title"
  another-thing: "This is another thing"

The above is untested but might point you in the right direction.

Hi @julesman -

This is definitely possible, and pretty straightforward.
I used a structure like this for a large project where there were 60 items in an art catalogue that each got their own page.

In the /data folder, I have a subdirectory called catalogue.
The contents look like this:

  • data/catalogue/1.yml
  • data/catalogue/2.yml
  • data/catalogue/3.yml
  • etc…

You can see the folder in question here. Any file in this folder can be accessed programatically via data.catalogue (returns the group) or data.catalogue.1 (returns a hash of the YAML contents of the file). When you access a single file this way, you get a hash to the effect of:
{ "filename" => file contents } where the file contents may be another hash, array, etc.

In the config.rb file, you can proxy through through data files like this:

data.catalogue.each do |cat, entry|
proxy “/catalogue/#{cat}.html”, “/catalogue/template.html”, :locals => {
:entry => entry
}, :ignore => true
end

So cat is the filename of the data file being accessed, and entry is the contents of the YAML file as a hash.
You can see the rest of the project’s source here, and the live site is here. This is an example page generated from the proxied YAML files.

best,

Eric

Thanks Eric! that’s amazing.

Definitely the way I am going to access the data files now.

Thanks so much for your response.

Jules.

Hey Craig, thanks very much for your response. I’ll give this a shot for sure and see if it works.

Thanks heaps,

Jules

My solution is use Yamlinc a tool to build multiple file into one with special tag $include take a look here https://github.com/javanile/yamlinc