2016-04-03 13 views
1

Ich versuche, eine Schleife in HAML zu erstellen, die mehrere Arrays verwenden könnte. Lass mich erklären, was ich meine.Schleifen mehrerer Arrays in HAML

Ich habe HAML Code, der wie folgt aussieht:

- ['thumb1', 'thumb2', 'thumb3', 'thumb4'].each_with_index do |value, index| 
    - ['head1', 'head2', 'head3', 'head4'].each do |i| 
    %span.span{:class => "photo-#{value}"} #{value} 
    %a #{i}ere 

Und es gibt dazu:

<span class='photo-thumb1 span'>thumb1</span> 
<a>head1</a> 
<span class='photo-thumb1 span'>thumb1</span> 
<a>head2</a> 
<span class='photo-thumb1 span'>thumb1</span> 
<a>head3</a> 
<span class='photo-thumb1 span'>thumb1</span> 
<a>head4</a> 
<span class='photo-thumb2 span'>thumb2</span> 
<a>head1</a> 
<span class='photo-thumb2 span'>thumb2</span> 
<a>head2</a> 
<span class='photo-thumb2 span'>thumb2</span> 
<a>head3</a> 
<span class='photo-thumb2 span'>thumb2</span> 
<a>head4</a> 
<span class='photo-thumb3 span'>thumb3</span> 
<a>head1</a> 
<span class='photo-thumb3 span'>thumb3</span> 
<a>head2</a> 
<span class='photo-thumb3 span'>thumb3</span> 
<a>head3</a> 
<span class='photo-thumb3 span'>thumb3</span> 
<a>head4</a> 
<span class='photo-thumb4 span'>thumb4</span> 
<a>head1</a> 
<span class='photo-thumb4 span'>thumb4</span> 
<a>head2</a> 
<span class='photo-thumb4 span'>thumb4</span> 
<a>head3</a> 
<span class='photo-thumb4 span'>thumb4</span> 

Aber der Ausgang ich wirklich will, ist:

<span class='photo-thumb1 span'>thumb1</span> 
<a>head1</a> 
<span class='photo-thumb2 span'>thumb1</span> 
<a>head2</a> 
<span class='photo-thumb3 span'>thumb1</span> 
<a>head3</a> 
<span class='photo-thumb4 span'>thumb1</span> 
<a>head4</a> 

Was bin Ich vermisse hier? Wenn ich versuche, das Leerzeichen zu löschen, würde es nur einen Fehler auslösen.

+0

Leider @ Alex, aber es ist nicht klar, was Sie wollen. –

+0

Möchten Sie wirklich, dass der Text für jeden der Bereiche in Ihrer Ausgabe 'thumb1' ist, oder' thumb1', 'thumb2' usw.? – matt

Antwort

1

Sorry, aber Ihre Frage ist so verwirrend. Es wäre etwas, was du brauchst?

- [['tb1', 'hd1'], ['tb2', 'hd2'], ['tb3', 'hd4']].each do |thumb, head| 
    %span{class: "photo-#{thumb} span"}= thumb 
    %a= head 
+0

Vielen Dank für Ihre Antwort. Aber weil ich versuche, den Prozess zu verstehen, möchte ich dich fragen: Was ist der Unterschied zwischen der .zip() - Methode und der, die du mir gegeben hast. Vielen Dank! – Alex

+0

Kein Unterschied, @Alex. Dieser Weg ist didaktischer, '.zip()' wird in diese Struktur _konvertiert. –

1

Ich denke, was Sie suchen zip ist:

- ['thumb1', 'thumb2', 'thumb3', 'thumb4'].zip(['head1', 'head2', 'head3', 'head4']) do |thumb, head| 
    %span.span{:class => "photo-#{thumb}"}= thumb 
    %a= head 
+0

Vielen Dank, matt! Es tut mir leid, ich habe es auf eine sehr verwirrende Art und Weise ausgedrückt. Es ist schwer für mich zu erklären, da ich neu bin. Die Antwort, die du mir gegeben hast, war genau die, nach der ich gesucht habe. – Alex

Verwandte Themen