Sending data class in to partial

I have data in a YAML file and I want to send it through to a partial.

-color1: Black
color2: White

<% data.colors.each do |color| %>
<%= partial “modules/#{lang}/cover/01/test”, locals: { color: color} %>
<% end %>

then I reference it by using colors[“variable name”]. Works perfectly but I don’t want to have all of the code in the first and last line.

something like this:
<%= partial “modules/#{lang}/cover/01/test”, locals: { data.colors: color} %>

but middleman spits errors when I try this.

What’s the right way to send a data object to a partial?

I didn’t read your question correctly in my previous reply. In your data YAML, you want to declare the colours as a list of items, like so:

colors:
  - black
  - white

Then, you can traverse through the list of colours the way you want:

<% data.colors.each do |color| %>
  ...
<% end %>

I think your first reply was closest to what I want, but I just don’t know ruby very well or how to implement it using erb templates. Basically right now I have 7 partials and the only difference is their color. I want to make it one partial and then say in a variable on the page I call the partial, say I want blue. I set the variable ‘color: blue’. Which then either sends the blue data file or the blue data part of the color data file into the partial to be used. I’m basically running into the problem that I have to adapt this for multiple partials that all have this variation but some use more of the blue color data and so say

- color: blue
  background: FFFFFF
  foreground: 000000
  text: 000004

or if the file was it’s own color file blue.yml inside the data/color folder

- background: FFFFFF
  foreground: 000000
  text: 000004

so that’s the blue color object but foreground doesn’t exist in say template 05 but exists in template 04. I don’t want to make several color templates for each specific purpose. I could do it through css classes, but I would end up making hundreds of classes. Ideally I could do it without a do, end loop as well because that adds complications to it and I’m adapting middleman for a purpose that is a bit unorthodox. Hopefully this clears things up on what I want to do!

You should be able to reference the local variable by just using its name color, no need for colors[]

So within the partial you can use "style" => "background-color: #{color};"

You may also want to add a check within the partial in case a color variable isn’t passed in.
- color ||= nil (basically if color exists then color = color else color = nil)