Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: working in a loop 1

Status
Not open for further replies.

kconmle

Programmer
Oct 20, 2009
18
0
0
US
I have no ruby experience - can anyone give me some guidance:

I have a loop where I am outputting a row of a table i.e.

<% for lef in @lefs %>
<tr>
<td></td>
</tr>
<%end%>

I want to have an if-else in there where I can change the class of the td. How would I go about that?
 
Hi

For example to create a table with the letters of the alphabet, applying the appropriate [tt]class[/tt] of 'vowel' or 'consonant' to the [tt]tr[/tt] of each letter :
Code:
<%

@lefs=('a'..'z').to_a
vowel='aeiou'

%>

<table>

<% for lef in @lefs %>
<tr class="<%=vowel.index(lef)?'vowel':'consonant' %>">
  <td><%=lef %></td>
</tr>
<% end %>

</table>
Or if you want an example with the use of an [tt]if[/tt] statement, this outputs only the vowels :
Code:
<%

@lefs=('a'..'z').to_a
vowel='aeiou'

%>

<table>

<% for lef in @lefs %>
  <% if vowel.index lef %>
<tr>
  <td><%=lef %></td>
</tr>
  <% end %>
<% end %>

</table>

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top