ruby - Basic string formatting in Rails view using loops -
hi newb rails here.
i want format string fit html div. model i'm after is;
- break string array seperated whitespace.
- if array item length greater 22
- split characters lengths of 22 , display.
- else separate words whitespace , display.
it's handling long names on guestbook type application.
<% $p = 0 %> <% @m = name.split(" ") %> <% while $p < @m.size %> <% if @m[$p].length > 22 %> <%= name.slice(0, 21) %><br> <%= name.slice(21, 43) %><br> <% else %> <% $i = 0 %> <% @x = name.split(" ") %> <% while $i < @x.size %> <%= @x[$i] %><br> <% $i +=1 %> <% end %> <% end %> <% $p +=1 %> <% end %>
(1..(name.length / 22)).each { |i| name[22*i] = ' ' } if name.length > 22
Comments
Post a Comment