Question

I have Slim template

# main.slim
- for item in items do
  td = item.a
  td = item.b

I have Ruby code

# main.rb
a = [1, 2, 3]
b = [4, 5, 6]

I want render HTML from this template.

I want show in result:

# main.html
<td>1</td><td>4</td>
<td>2</td><td>5</td>
<td>3</td><td>6</td>

How I can put "a" and "b" arrays in "items" variable (or object)?

I render from Ruby:

Slim::Template.new("main.slim").render(nil, :items => items)
Was it helpful?

Solution

Zip will pack up n arrays of length m into an array of m tuples where each tuple has n-elements. Tuples are just arrays as opposed to objects w/ named fields. That said, this could make it easier to iterate over.

items = a.zip(b)
for item in items do
  td = item[0]
  td = item[1]
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top