ruby - Rails: Split and wrap each character in a string? -
I have something like this:
& lt;% = @ votes% & gt; ;
Outputs just like a string:
123
Is it easy to wrap each letter Some HTML with the helpful way?
& lt; Span class = "outerclass" & gt; & Lt; Span class = "innerclass" & gt; 1 & lt; / Span & gt; & Lt; / Span & gt; & Lt; Span class = "outerclass" & gt; & Lt; Span class = "innerclass" & gt; 2 & lt; / Span & gt; & Lt; / Span & gt; & Lt; Span class = "outerclass" & gt; & Lt; Span class = "innerclass" & gt; 3 & lt; / Span & gt; & Lt; / Span & gt;
You can make such a helper:
def wrap_each_char (string, and block) string.each_char.map (& block). Add End
Then use it like this (erb):
<% - @votes = "123" -%> ; & Lt;% - wrap_each_char (@votes). C | -% & gt; & Lt; Span class = "external" & gt; & Lt; Span class = "inner" & gt; & Lt;% = c% & gt; & Lt; / Span & gt; & Lt; / Span & gt; & Lt;% - end -% & gt;
Update: The above example shows a helpful way to pass a block level content, but it is worth noting that this particular example Can be simplified by simply using the every_church
method on the string and by completely avoiding the support:
& lt;% - @ votes.each_char do | C | -% & gt; & Lt; Span class = "external" & gt; & Lt; Span class = "inner" & gt; & Lt;% = c% & gt; & Lt; / Span & gt; & Lt; / Span & gt; & Lt;% - end -% & gt;
Comments
Post a Comment