Reputation: 3553
Is it possible to iterate through a data file using tags and categories based on a product data file? E.g.:
# toys.yml
- name: Fire Truck
id: 1
description: Red
category: Automobile
url: toys/fire-truck
tags: red, truck
- name: Freight Train
id: 2
description: Fast delivery mail
url: toys/freight-train
category: Train
tags: freight, train, rail
I'm using Proxy pages to generate pages.
data.toys.each do |t|
proxy toys.path, "toys.html", locals: { toy: t}, ignore: true
end
The index.html.erb
template would be:
<div class="toys">
<% data.toys.each do |t| %>
<h1><%= t.name %></h1>
<p><%= t.desription %></p>
<span class="category"><%= t.category %></span> // I would like this to be linked to generate categories based on the toys.yml file
<span class="tags"><%= t.tags %></span> // The same as category, generated tag pages based on the toys.yml
<% end %>
</div>
How can I do this? Should I just create:
category.yml
can populate it with the categories, is there a way to link it to toys.yml
unique id?I'm learning about static pages and wanted to know ways to implement this without building a db backed app.
Upvotes: 1
Views: 219
Reputation: 771
You can create a category.html.erb
file and proxy that so that you create a category page for each unique category in the toys.yml file
categories.each do |c|
proxy "#{c}.html", "category.html", locals: { category: c, toys: data.toys}, ignore: true
end
As for how you get the list of categories, you can use standard ruby operations on the data.toys list e.g.
categories = data.toys.map {|t| t.category}.uniq
Tags would be similar but you would need to parse the comma separated list.
You can also build more complex data types or define your own Ruby Toy and Category classes which you then pass to the proxy call in the locals argument. This would allow you to for example, have category objects contain a list of toy objects which belong to that category. The ruby Enumerable group_by
method would be useful in that case.
Upvotes: 1