Hello,
On my site, I have a group of projects that I list out on a ‘projects’ page. Each project then has its own individual page, with a shared template.
The data structure is:
data/projects.yml
project_one
title:
"Project One"
description:
"Description of project one"
year:
2018
project_two
title:
"Project Two"
description:
"Description of project two"
year:
2018
I then manually list each project on the ‘projects’ page, and reuse the data on an individual page, both with:
<%= data.projects.project_two.title %>
This works at the moment, but it doesn’t scale well and any changes made to a project’s markup in the list has to be duplicated throughout the page.
I can easily change this to an array and loop through it, e.g.
data/projects.yml
- project_one
title:
"Project One"
description:
"Description of project one"
year:
2018
- project_two
title:
"Project Two"
description:
"Description of project two"
year:
2018
<% unless data.work.nil? %>
<ul>
<% data.work.each do |f| %>
<li>
<h2><%= f.title %></h2>
<p><%= f.description %></p>
</li>
<% end %>
</ul>
<% end %>
I’d then like to access specific project values for each project page, so, for example, if I have project-one.html.erb
, I can display the title from the array.
I know this is possible with
<%= data.work[0].title %>
<%= data.work[0].description %>
But is it possible to access the hash value by key name? I’ve read a number of posts on the forum, and referenced some Ruby docs, and have seen references to something like:
<%= data.work['project_two']['title'] %>
but that returns a no implicit conversion of String into Integer
error, as I imagine its expecting an array integer. I have a basic understanding of Ruby methods but not certain what applies here.
The docs mention something similar using a proxy and auto-generated pages, but each of my project pages are heavily customized so generating on build isn’t an option. Alternatively, I’ve looked at building each project as a yaml file in a subdirectory (data/projects/project-one.yml
) and then accessing them individually, both I’m not sure it’s feasible to loop through and output the subdirectories on an index page.
Thanks for any tips