Select specific item in YAML with HAML
Closed this issue · 1 comments
Problem
I need to be able to select a specific entry in my yml-file within a HAML-file.
Background
So, I know how to iterate over the contents of the yml-file with - data.projects.each do |project|
but how do I only select a specific entry? I've added a field called ID in my yml-file so that I can differentiate between the entries - Now how do I go about to only select the content with a specific ID ?
My yml-file looks like this;
- id: 1 name: FizzBuzz short_description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vehicula sem sit amet risus pellentesque. coverimage: fizzbuzz-cover date: 2018-01-05
Any idéas? :)
So, Raoul the wise stepped in as usual and explained it to me - To select a specific entry from a yml-file you can do something like this in you haml-file;
- project = data.projects.select { |project| project[:id] == 1 }.first
%h1= project[:name]
You save the entry in a new variable, in this case "project" and what you save is an array. The logic behind is that it saves the "first" entry that fits the argument, in this case id == 1